📄 stdio.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>Standard I/O Streams</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><blockquote><center><h3><a name = "tag_000_009"> </a>Standard I/O Streams</h3></center><xref type="2" name="streams"></xref>A stream is associated with an external file (which may be a physicaldevice) by<i>opening</i>a file, which may involve<i>creating</i>a new file.Creating an existing file causes its former contents to bediscarded if necessary.If a file can support positioning requests,(such as a disk file, as opposed to a terminal),then a<i>file position indicator</i>associated with the stream is positioned at the start(byte number 0)of the file, unless the file is opened with append mode,in which case it is implementation-dependent whether thefile position indicatoris initially positioned at the beginning or end of the file.The file position indicator is maintained by subsequent reads,writes and positioning requests, to facilitate an orderlyprogression through the file.All input takes place as if bytes were read by successive calls to<i><a href="fgetc.html">fgetc()</a></i>;all outputtakes place as if bytes were written by successive calls to<i><a href="fputc.html">fputc()</a></i>.<p>When a stream is<i>unbuffered</i>,bytes are intended to appear from the source or at thedestination as soon as possible.Otherwise bytes may be accumulated and transmittedas a block.When a stream is<i>fully buffered</i>,bytes are intended to be transmittedas a block when a buffer is filled.When a stream is<i>line buffered</i>,bytes are intended to be transmittedas a block when a newline byte isencountered.Furthermore, bytes are intended to be transmitted as a blockwhen a buffer is filled, when input isrequested on an unbuffered stream, or when input is requested ona line-buffered stream that requires the transmission ofbytes.Support for these characteristics is implementation-dependent,and may be affected via<i><a href="setbuf.html">setbuf()</a></i>and<i><a href="setvbuf.html">setvbuf()</a></i>.<p>A file may be disassociated from a controlling stream by<i>closing</i>the file.Output streams are flushed(any unwritten buffer contents are transmitted)before the stream is disassociated from the file.The value of a pointer to aFILEobject is indeterminate after the associated file is closed(including the standard streams).<p>A file may be subsequently reopened,by the same or another program execution,and its contents reclaimed or modified(if it can be repositioned at its start).If the<i>main()</i>function returns to its original caller,or if the<i><a href="exit.html">exit()</a></i>function is called, all open files are closed(hence all output streams are flushed)before program termination.Other paths to program termination,such as calling<i><a href="abort.html">abort()</a></i>,need not close all files properly.<p>The address of the FILE object used to control a stream may be significant; acopy of a FILE object need not necessarily serve in place of the original.<p>At program startup, three streams are predefined and need notbe opened explicitly:<i>standard input</i>(for reading conventional input),<i>standard output</i>(for writing conventional output), and<i>standard error</i>(for writing diagnostic output).When opened, the standard error stream is not fully buffered;the standard input and standard output streams are fully bufferedif and only if the stream can be determined not to refer to aninteractive device.<h4><a name = "tag_000_009_001"> </a>Interaction of File Descriptors and Standard I/O Streams</h4><xref type="3" name="interaction"></xref>An open file description may be accessed through afile descriptor, which is created using functions such as<i><a href="open.html">open()</a></i>or<i><a href="pipe.html">pipe()</a></i>,or through a stream, which is created using functions such as<i><a href="fopen.html">fopen()</a></i>or<i><a href="popen.html">popen()</a></i>.Either a file descriptor or a stream willbe called a<i>handle</i>on the open file description to which it refers; an open file descriptionmay have several handles.<p>Handles can be created or destroyed by explicit user action, withoutaffecting the underlying open file description. Some of the ways tocreate them include<i><a href="fcntl.html">fcntl()</a></i>,<i><a href="dup.html">dup()</a></i>,<i><a href="fdopen.html">fdopen()</a></i>,<i><a href="fileno.html">fileno()</a></i>and<i><a href="fork.html">fork()</a></i>.They can be destroyed by at least<i><a href="fclose.html">fclose()</a></i>,<i><a href="close.html">close()</a></i>and the<i>exec</i>functions.<p>A file descriptor that is never used in an operation that could affectthe file offset (for example,<i><a href="read.html">read()</a></i>,<i><a href="write.html">write()</a></i>or<i><a href="lseek.html">lseek()</a></i>)is not considered a handle for this discussion, but couldgive rise to one (for example, as a consequence of<i><a href="fdopen.html">fdopen()</a></i>,<i><a href="dup.html">dup()</a></i>or<i><a href="fork.html">fork()</a></i>).This exception does not include the file descriptorunderlying a stream, whether created with<i><a href="fopen.html">fopen()</a></i>or<i><a href="fdopen.html">fdopen()</a></i>,so long as it is not used directly by the application to affectthe file offset. The<i><a href="read.html">read()</a></i>and<i><a href="write.html">write()</a></i>functions implicitly affect the file offset;<i><a href="lseek.html">lseek()</a></i>explicitly affects it.<p>The result of function calls involving any one handle (the<i>active handle</i>)are defined elsewhere in this specification, but if two or more handlesare used, and any one of them is a stream, their actions must becoordinated as described below.If this is not done, theresult is undefined.<p>A handle which is a stream is considered to be closed when either an<i><a href="fclose.html">fclose()</a></i>or<i><a href="freopen.html">freopen()</a></i>is executed on it (the result of<i><a href="freopen.html">freopen()</a></i>is a new stream, which cannot be a handle onthe same open file description as its previous value), or when theprocess owning that stream terminates with<i><a href="exit.html">exit()</a></i>or<i><a href="abort.html">abort()</a></i>.A file descriptor is closed by<i><a href="close.html">close()</a></i>,<i><a href="_exit.html">_exit()</a></i>or the<i>exec</i>functions when FD_CLOEXEC is set on that file descriptor.<p>For a handle to become the active handle, the actions below mustbe performed between the last use of the handle (the currentactive handle) and the first use of the secondhandle (the future active handle). The second handle then becomesthe active handle. All activity by the application affecting the fileoffset on the first handle must be suspended until it again becomes theactive file handle. (If a stream function has as an underlyingfunction one that affects the file offset, the stream function willbe considered to affect the file offset.)<p>The handles need not be in the same process for these rules to apply.<p>Note that after a<i><a href="fork.html">fork()</a></i>,two handles exist where one existed before.The application must assure that,if both handles will ever be accessed,that they will both be in a state where the other couldbecome the active handle first.The application must prepare for a<i><a href="fork.html">fork()</a></i>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -