- ã¯ããã«(注æäºé ç)
- åºæ¬
- æ¨æºå ¥åºå
- ãã¡ã¤ã«
- ãã£ã¬ã¯ããª
- ã·ã°ãã«
- æé
- ã¡ã¢ãª
- ã·ã¹ãã å¨ã
- ããã»ã¹ã¨IPC
- æ£è¦è¡¨ç¾
- ã¹ã¬ãã
- ã³ã³ãã¤ã«
- ã©ã¤ãã©ãª
- ãããã°
æ¨æºå ¥åºå(1)
ãã¤ã¼ãæ¨æºå ¥åºåã¯Cè¨èªããåºæ¥ãã°èª°ã§ãç¥ã£ã¦ããç°¡åãªä»çµã¿ã§ãããUnixç³»OSã§ã¯é常ã«æ·±ãé¢ãã£ã¦ããä»çµã¿ã§ãã®ã§ä¸ã¤ä¸ã¤èª¬æãã¦ããã¾ããããã§ã¯ã¯Cè¨èªã®æ¨æºé¢æ°ã使ç¨ãã¦èª¬æãã¦ããã¾ãã
æ¨æºå ¥åºåã¨ã¯
æ¨æºå ¥åºåã¨ã¯"é常"ããã¼ãã¼ãããã®å ¥åãã¿ã¼ããã«ã¸ã®åºåã®ãã¨ã§ã(ã¾ããCè¨èªã®è¦æ ¼ã§ã¯ã©ãããå ¥åããããã©ãã¸åºåãããã決ã¾ã£ã¦ãã¾ããã...)ãWidowsã§ã¯GUIãå½ããåã§ã»ã¨ãã©ä½¿ç¨ãã¾ããããUnixã§ã¯ãããã®æ©è½ä¾ãã°lsã³ãã³ããå®è¡ããçµæã¯æ¨æºåºåã使ç¨ãã¦ãã¾ããsortãcutçãªã©æ§ã ãªã³ãã³ããå©ç¨ãã大åãªæ¦å¿µã§ãã
æ¨æºå ¥åºåã¯3種é¡
æ¨æºå ¥åºåã«ã¯3種é¡ããã¾ãã
åå | 説æ |
---|---|
æ¨æºå ¥å | stdin |
æ¨æºåºå | stdout |
æ¨æºã¨ã©ã¼ | stderr |
ã¾ãã¯åºæ¬ã®Hello Worldãã
ã¾ãã¯Hello Woldã¨ã³ã³ãã¤ã«ã®Hello Worldãå ã«ãããã解説ãã¦ããã¾ãã®ã§ãããä¸åº¦è¦ã¦ã¿ã¾ãã
//helloworld.c #include <stdio.h> int main(void) { printf("Hello Wold\n"); return 0; }
ã³ã³ãã¤ã«ã¨å®è¡ããã¾ããã¨ãã«åé¡ããã¾ããã
$ gcc -o helloworld helloworld.c $ ./helloworld Hello World
æ¨æºåºåã¨æ¨æºã¨ã©ã¼
æ¨æºåºå
ãã¦ããã®Hello Worldãstdoutã使ãããã«æ¸ãæãã¾ããæ¨æºå ¥åºåãæ示çã«æå®ããã«ã¯fprintf(3)ã使ç¨ãã¾ãã
//helloworld_stdout.c #include <stdio.h> int main(void) { fprintf(stdout, "Hello Wold\n"); return 0; }
ã³ã³ãã¤ã«ã¨å®è¡ããã¾ããå®è¡çµæã¯printf(3)çã®Hello Worldã¨åãã¯ãã§ãã
$ gcc -o helloworld_stdout helloworld_stdout.c $ ./helloworld_stdout Hello World
æ¨æºã¨ã©ã¼
次ã¯æ¨æºã¨ã©ã¼ã§ãã
//helloworld_stderr.c #include <stdio.h> int main(void) { fprintf(stderr, "Hello Wold\n"); return 0; }
ã³ã³ãã¤ã«ã¨å®è¡ããã¾ãã
$ gcc -o helloworld_stderr helloworld_stderr.c $ ./helloworld_stderr Hello World
å®è¡çµæã¯stdoutçã¨éããç¡ãããã«è¦ãã¾ããå®ã¯éãã®ã§ãã
åºåã¨Shell
ããã確ãããçºã«æ¬¡ã®ããã«å®è¡ãã¦ã¿ã¦ãã ããã
$ ./helloworld_stdout < result_stdout $ ./helloworld_stderror < result_stderror Hello World $ cat result_stdout Hello World $ cat result_stderror $
人éã®ç®ã§è¦ãã¨éãã¯åããã¾ããããã®ããã«ããã¨ä¸ç®çç¶ã§ããstdoutã¯é常ã®åºåããã¾ãããstderrã¯ã¨ã©ã¼å°ç¨ã®åºåã¸ã¨åºåãã¾ããshellã¯ããããç解ãã¾ãã ">"ã¯æ¨æºåºåã対象ã«ãªãã¾ãã詳ããã¯Unixã¨Cè¨èªã®é£æºãè¦ã¦ãã ããã
æ¨æºå ¥å
ã¾ãã¯æç§æ¸ã©ããã«scanf(3)ã§
次ã¯æ¨æºå ¥åã§ããã¨ãããããå ¥éãªã®ã§scanf(3)ã使ãã¾ããscanf(3)ã¯å¤ã åé¡ãããã¾ãããä¾é¡ãªã®ã§æ§ããªãã§ãããã
//input.c #include <stdio.h> int main(void) { int x; scanf("%d", &x); fprintf(stdout, "get:%d\n", x); return 0; }
ã³ã³ãã¤ã«ã¨å®è¡ããã¦ã¿ã¾ããæç§æ¸ã©ããã®åä½ã§ããã
$ gcc -o input input.c $ ./stdin 1 get:1
stdinã使ç¨ãã¦ã¿ãã
ãã¦ãstdoutãstderrã¨åããstdinã使ç¨ããããã«å¤æãã¾ããprintf(3)ãfprintf(3)ã«ããããã«stdinã®å ´åã¯scanf(3)ãfscanf(3)ã«ãã¾ãã
//input_stdin.c #include <stdio.h> int main(void) { int x; fscanf(stdin, "%d", &x); fprintf(stdout, "get:%d\n", x); return 0; }
ã³ã³ãã¤ã«ã¨å®è¡ããã¦ã¿ã¾ããinput.cã¨åãåä½ã«ãªãäºã確èªãã¦ãã ããã
$ gcc -o input_stdin input_stdin.c $ ./stdin 1 get:1
é¢æ°ã¨ãæ¨æºå ¥åºåã®é¢ä¿
é¢æ°ã®åä»ãã«ã¼ã«
printf(3)ãfprintf(3)ã«ãscanf(3)ãfscanf(3)ã«ããããã«å
¥åºåãæå®ããã«ã¯é ã«"f"ãä»ãã¾ãã
ä¾ãã°ãgetc(3)ã«ã¯fgetc(3)ã«ã¨ãã£ãå
·åã§ãã
æ¨æºå ¥åºåã£ã¦ããããä½?
ããããæ¨æºå ¥åºåã¨ã¯ä½ãªã®ã§ãããã? ã¾ãã¯fprintf(3)ã®ãããã¿ã¤ããè¦ã¦ã¿ã¾ãããã
#include <stdio.h> int fprintf(FILE *stream, const char *format, ...);
stdinçã¯FILE*åã®ããã§ããã¤ã¾ãåãªããã¤ã³ã¿ã§ãããã¡ã¤ã«å ¥åºåã§ã¯fopen(3)ã®æ»ãå¤ãfprintf(3)ã«æ¸¡ãã¦ä½¿ãã¾ãããæ¨æºå ¥åºåã¯ãã¡ã¤ã«ã¨ä½ã代ãããç¡ãã®ã§ãã
ã¤ã¾ããå ¥åºåå ããã¡ã¤ã«ãæ¨æºå ¥åºåãã ãã§ããUNIXã®ç¹å¾´ã®1ã¤ããã¡ã¤ã«ããã¼ãã¼ããã¿ã¼ããã«ããã¡ã¤ã«ã¨ãã1ã¤ã®æ¦å¿µã«æ½è±¡åãè¡ã£ãç¹ã§ãã
ããã«ãããããã°ã©ãã¯ãã¡ã¤ã«ããã¼ãã¼ããå¿ è¦ä»¥ä¸æèããªãã¦ããããããã°ã©ã ãfprintf(3)ã®ç¬¬ä¸å¼æ°ãå¤æ´ããã ãã¨ããé常ã«ç°¡åãªããã°ã©ã ããããã®ã§ãã
ã¾ã¨ã
次ã¯ãã®ãã¼ã¸ã§ä½ã£ãããã°ã©ã ã§ä½¿ç¨ããæ¨æºé¢æ°ãã·ã¹ãã ã³ã¼ã«ã«ç½®ãæãã¦è¡ãã¾ãããããUnixç³»OSãæ±ãçºã®ç¬¬ä¸æ©ã¨ãªãã¾ãã
SBã¯ãªã¨ã¤ãã£ã
売ãä¸ãã©ã³ãã³ã°: 90927
ç§åã·ã¹ãã
売ãä¸ãã©ã³ãã³ã°: 21974