1 .\" From dholland@burgundy.eecs.harvard.edu Tue Mar 24 18:08:15 1998
3 .\" This man page was written in 1998 by David A. Holland
4 .\" and placed in the Public Domain. Polished a bit by aeb.
5 .\" 2005-06-16 mtk, mentioned freopen()
7 .\" 2007-12-08, mtk, Converted from mdoc to man macros
9 .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
10 .\" Translated 1998-03-22, NAKANO Takeo <nakano@apm.seikei.ac.jp>
11 .\" Updated 2005-09-04, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
13 .TH STDIN 3 2008-07-14 "Linux" "Linux Programmer's Manual"
15 .\"O stdin, stdout, stderr \- standard I/O streams
17 stdin, stdout, stderr \- ɸ½àÆþ½ÐÎÏ¥¹¥È¥ê¡¼¥à
23 .BI "extern FILE *" stdin ;
24 .BI "extern FILE *" stdout ;
25 .BI "extern FILE *" stderr ;
29 .\"O Under normal circumstances every UNIX program has three streams opened
30 .\"O for it when it starts up, one for input, one for output, and one for
31 .\"O printing diagnostic or error messages.
32 .\"O These are typically attached to
33 .\"O the user's terminal (see
35 .\"O but might instead refer to files or other devices, depending on what
36 .\"O the parent process chose to set up.
37 .\"O (See also the "Redirection" section of
39 ɸ½à¤Î´Ä¶¤Ç¤Ï¡¢ UNIX ¥×¥í¥°¥é¥à¤Ïµ¯Æ°»þ¤Ë¡¢¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¥¹¥È¥ê¡¼
40 ¥à¤ò 3 ¤ÄÍ¿¤¨¤é¤ì¤ë¡£¤½¤ì¤¾¤ìÆþÎÏÍÑ¡¢½ÐÎÏÍÑ¡¢¿ÇÃÇ¥á¥Ã¥»¡¼¥¸¤ä¥¨¥é¡¼¥á¥Ã
41 ¥»¡¼¥¸¤Îɽ¼¨ÍѤΤâ¤Î¤Ç¤¢¤ë¡£¤³¤ì¤é¤ÏÄ̾ï¥æ¡¼¥¶¤ÎüËö
43 ¤ò¸«¤è) ¤ËÀܳ¤µ¤ì¤Æ¤¤¤ë¤¬¡¢¿Æ¥×¥í¥»¥¹¤Ç¤ÎÁªÂò¡¦ÀßÄê¤Ë¤è¤Ã¤Æ¥Õ¥¡¥¤¥ë
44 ¤ä¾¤Î¥Ç¥Ð¥¤¥¹¤Ë´ØÏ¢¤Å¤±¤é¤ì¤Æ¤¤¤ë¤³¤È¤â¤¢¤ë
46 ¤Î¡Ö¥ê¥À¥¤¥ì¥¯¥·¥ç¥ó¡×¥»¥¯¥·¥ç¥ó¤â»²¾È¤Î¤³¤È)¡£
48 .\"O The input stream is referred to as "standard input"; the output stream is
49 .\"O referred to as "standard output"; and the error stream is referred to
50 .\"O as "standard error".
51 .\"O These terms are abbreviated to form the symbols
52 .\"O used to refer to these files, namely
57 ÆþÎÏ¥¹¥È¥ê¡¼¥à¤Ï "standard input" ¤È¸Æ¤Ð¤ì¤ë¡£½ÐÎÏ¥¹¥È¥ê¡¼¥à¤Ï
58 "standard output"¡¢¥¨¥é¡¼¥¹¥È¥ê¡¼¥à¤Ï "standard error" ¤È¸Æ¤Ð¤ì¤ë¡£
59 ¤³¤ì¤é¤ÎÍѸì¤òû½Ì¤·¤¿¤â¤Î¤¬¤½¤ì¤¾¤ì¤Î¥Õ¥¡¥¤¥ë¤ò¼¨¤¹¥·¥ó¥Ü¥ë¤È¤Ê¤ë¡£¤Ä
66 .\"O Each of these symbols is a
68 .\"O macro of type pointer to FILE, and can be used with functions like
74 ¤Î¥Þ¥¯¥í¤Ç¡¢ FILE ¤Ø¤Î¥Ý¥¤¥ó¥¿·¿¤Ç¤¢¤ë¡£¤·¤¿¤¬¤Ã¤Æ
78 ¤Ê¤É¤Î´Ø¿ô¤È¤È¤â¤ËÍѤ¤¤ë¤³¤È¤¬¤Ç¤¤ë¡£
82 .\"O are a buffering wrapper around UNIX file descriptors, the
83 .\"O same underlying files may also be accessed using the raw UNIX file
84 .\"O interface, that is, the functions like
89 ¤Ï UNIX ¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë¥Ð¥Ã¥Õ¥¡µ¡Ç½¤òÄɲä·¤¿¥é¥Ã¥Ñ¡¼
90 ¤Ç¤¢¤ë¤«¤é¡¢¤³¤ì¤é¤Î¥Þ¥¯¥í¤Ë¤âÂбþ¤¹¤ë¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¡¢ UNIX ¤Î raw ¥Õ¥¡
95 ¤Ê¤É) ¤Ë¤è¤Ã¤Æ¥¢¥¯¥»¥¹¤¹¤ë¤³¤È¤â¤Ç¤¤ë¡£
97 .\"O On program startup, the integer file descriptors
98 .\"O associated with the streams
103 .\"O are 0, 1, and 2, respectively.
104 .\"O The preprocessor symbols
105 .\"O .BR STDIN_FILENO ,
106 .\"O .BR STDOUT_FILENO ,
108 .\"O .B STDERR_FILENO
109 .\"O are defined with these values in
110 .\"O \fI<unistd.h>\fP.
113 .\"O to one of these streams can change the file descriptor number
114 .\"O associated with the stream.)
115 ¥×¥í¥°¥é¥à¤Îµ¯Æ°»þ¤Ë¤Ï¡¢
120 ¤Ë·ë¤Ó¤Ä¤±¤é¤ì¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÈÖ¹æ¤Ï¡¢
121 ¤½¤ì¤¾¤ì 0, 1, 2 ¤Ç¤¢¤ë¡£
122 ¥×¥ê¥×¥í¥»¥Ã¥µ¥·¥ó¥Ü¥ë
128 Ãæ¤Ç¤½¤ì¤¾¤ì¤³¤ì¤é¤ÎÃͤËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
129 (¤³¤ì¤é¤Î¥¹¥È¥ê¡¼¥à¤ËÂФ·¤Æ
131 ¤òŬÍѤ¹¤ë¤³¤È¤Ç¡¢¤½¤Î¥¹¥È¥ê¡¼¥à¤Ë´ØÏ¢ÉÕ¤±¤é¤ì¤¿¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿
132 ¤ÎÈÖ¹æ¤òÊѹ¹¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£)
134 .\"O Note that mixing use of
136 .\"O and raw file descriptors can produce
137 .\"O unexpected results and should generally be avoided.
138 .\"O (For the masochistic among you: POSIX.1, section 8.2.3, describes
139 .\"O in detail how this interaction is supposed to work.)
140 .\"O A general rule is that file descriptors are handled in the kernel,
141 .\"O while stdio is just a library.
142 .\"O This means for example, that after an
144 .\"O the child inherits all open file descriptors, but all old streams
145 .\"O have become inaccessible.
147 ¤È raw ¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÊ»ÍѤϡ¢Í½´ü¤Ç¤¤Ê¤¤·ë²Ì¤òÀ¸¤¸
148 ¤ë¤³¤È¤¬¤¢¤ë¤Î¤Ç¡¢Ä̾ï¤ÏÈò¤±¤ë¤Ù¤¤Ç¤¢¤ë¡£ (¥Þ¥¾¥Ò¥¹¥Æ¥£¥Ã¥¯¤Ê¿Í¤Ë:
149 POSIX.1 ¤Î¥»¥¯¥·¥ç¥ó 8.2.3 ¤Ë¤Ï¡¢¤³¤Îº®ÍѤÇÆ°ºî¤¬¤É¤Î¤è¤¦¤Ë¤Ê¤ê¤½¤¦
150 ¤«¤¬¾Ü¤·¤¯µ½Ò¤µ¤ì¤Æ¤¤¤ë¡£)
151 °ìÈÌŪ¤Ê¥ë¡¼¥ë¤Ï°Ê²¼¤ÎÄ̤ê: ¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¥«¡¼¥Í¥ë¤Ë¤è¤Ã¤Æ
152 °·¤ï¤ì¡¢ stdio ¤Ïñ¤Ë¥é¥¤¥Ö¥é¥ê¤Ë¤è¤Ã¤Æ°·¤ï¤ì¤ë¤Î¤Ç¤¢¤ë¡£¤¹¤Ê¤ï¤ÁÎ㤨¤Ð
154 ¤Î¸å¤Ë¤Ï¡¢»Ò¥×¥í¥»¥¹¤Ï¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿
155 ¤ò¤¹¤Ù¤Æ·Ñ¾µ¤¹¤ë¤¬¡¢¿Æ¤«¤é¤Î¥¹¥È¥ê¡¼¥à¤Ï¤¹¤Ù¤Æ¥¢¥¯¥»¥¹ÉԲĤȤʤ롣
157 .\"O Since the symbols
162 .\"O are specified to be macros, assigning to them is nonportable.
163 .\"O The standard streams can be made to refer to different files
164 .\"O with help of the library function
165 .\"O .BR freopen (3),
166 .\"O specially introduced to make it possible to reassign
171 .\"O The standard streams are closed by a call to
173 .\"O and by normal program termination.
178 ¤Ï¤¹¤Ù¤Æ¥Þ¥¯¥í¤È¤·¤ÆÄêµÁ¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤ì¤é¤Ø¤ÎÂåÆþ
179 ¤Ï°Ü¿¢À¤òÊݾڤµ¤ì¤Ê¤¤¡£É¸½à¥¹¥È¥ê¡¼¥à¤Ï¥é¥¤¥Ö¥é¥ê´Ø¿ô
181 ¤òÍѤ¤¤ì¤Ð¡¢Ê̤Υե¡¥¤¥ë¤ò¼¨¤¹¤è¤¦¤ËÊѹ¹¤¹¤ë¤³¤È¤â¤Ç¤¤ë¡£
186 ¤ÎºÆ³ä¤êÅö¤Æ¤¬²Äǽ¤Ê¤è¤¦¤ËÆÃÊ̤ËƳÆþ¤µ¤ì¤¿¤â¤Î¤Ç¤¢¤ë¡£
189 ¤Î¸Æ¤Ó½Ð¤·¤È¡¢¥×¥í¥°¥é¥à¤ÎÀµ¾ï½ªÎ»¤Ë¤è¤Ã¤Æ¥¯¥í¡¼¥º¤µ¤ì¤ë¡£
190 .\"O .SH "CONFORMING TO"
197 .\"O macros conform to C89
198 .\"O and this standard also stipulates that these three
199 .\"O streams shall be open at program startup.
203 ¥Þ¥¯¥í¤Ï C89 ¤Ë½àµò¤·¤Æ¤¤¤ë¡£
204 ¤Þ¤¿ C89 ¤Ç¤Ï¡¢¤³¤ì¤é 3 ¤Ä¤Î¥¹¥È¥ê¡¼¥à¤¬¥×¥í¥°¥é¥à
205 ¤Îµ¯Æ°»þ¤Ë¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¤Ù¤¤Ç¤¢¤ë¤³¤È¤¬µ¬Äꤵ¤ì¤Æ¤¤¤ë¡£
213 .\"O is line-buffered when it points to a terminal.
214 .\"O Partial lines will not
219 .\"O is called, or a newline is printed.
220 .\"O This can produce unexpected
221 .\"O results, especially with debugging output.
223 ¥¹¥È¥ê¡¼¥à¤Ï¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
225 ¥¹¥È¥ê¡¼¥à¤Ï¡¢Ã¼Ëö¤ËÀܳ¤µ¤ì¤Æ¤¤¤ë¤È¤¤Ë¤Ï¹Ôñ°Ì¤Ç¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°
226 ¤µ¤ì¤Æ¤¤¤ë¡£°ì¹Ô¤ËËþ¤¿¤Ê¤¤ÆâÍƤϡ¢
230 ¤¬¸Æ¤Ó½Ð¤µ¤ì¤ë¤«¡¢²þ¹Ôʸ»ú¤¬°õ»ú¤µ¤ì¤ë¤Þ¤Çɽ¼¨¤µ¤ì¤Ê¤¤¡£¤³¤ì¤Ï¡¢
231 Æä˥ǥХå°»þ¤Ë¤ª¤¤¤Æ¡¢Í½´ü¤·¤Ê¤¤·ë²Ì¤òÀ¸¤¸¤ë¸¶°ø¤È¤Ê¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
232 .\"O The buffering mode of the standard streams (or any other stream)
233 .\"O can be changed using the
238 ɸ½à¥¹¥È¥ê¡¼¥à¤Î (¤¢¤ë¤¤¤Ï¾¤Î¤¹¤Ù¤Æ¤Î¥¹¥È¥ê¡¼¥à¤Î)
239 ¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¥â¡¼¥É¤Ï¡¢
243 ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤Ë¤è¤Ã¤ÆÊѹ¹¤Ç¤¤ë¡£
244 .\"O Note that in case
246 .\"O is associated with a terminal, there may also be input buffering
247 .\"O in the terminal driver, entirely unrelated to stdio buffering.
248 .\"O (Indeed, normally terminal input is line buffered in the kernel.)
249 .\"O This kernel input handling can be modified using calls like
250 .\"O .BR tcsetattr (3);
254 .\"O .BR termios (3).
257 ¤¬Ã¼Ëö¤ËÀܳ¤µ¤ì¤Æ¤¤¤ë¤È¤¤Ï¡¢Ã¼Ëö¤Î¥É¥é¥¤¥Ð¤Ç¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤µ¤ì¤Æ¤¤¤ë
258 ²ÄǽÀ¤¬¤¢¤ëÅÀ¤Ë¤âÃí°Õ¤¹¤ë¤³¤È¡£¤³¤ì¤Ï stdio ¤Î¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤È¤ÏÁ´¤¯
259 ´Ø·¸¤Ê¤¯Â¸ºß¤·¤¦¤ë¡£ (¼ÂºÝ¡¢Ä̾ï¤À¤ÈüËöÆþÎϤϥ«¡¼¥Í¥ë¤Ë¤è¤Ã¤Æ¹Ôñ°Ì
260 ¤Ç¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤µ¤ì¤Æ¤¤¤ë¡£) ¤³¤Î¥«¡¼¥Í¥ë¤Ë¤è¤ëÆþÎϤΰ·¤¤¤Ï
262 ¤Ê¤É¤Î¸Æ¤Ó½Ð¤·¤Ë¤è¤Ã¤ÆÊѹ¹¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£