📄 fopen.3s
字号:
.\" SCCSID: @(#)fopen.3s 8.1 9/11/90.TH fopen 3s .SH Namefopen, freopen, fdopen \- open a stream.SH Syntax.B #include <stdio.h>.PP.B FILE.B \(**\fIfopen (filename, type\fP).br.B char \(**\fIfilename, \(**type\fP;.PP.B FILE.B \(**\fIfreopen (filename, type, stream\fP).br.B char \(**\fIfilename, \(**type\fP;.br.B FILE.B \(**\fIstream\fP;.PP.B FILE.B \(**\fIfdopen (fildes, type\fP).br.B int \fIfildes\fP;.br.B char \(**\fItype\fP;.SH Description.NXR "fopen subroutine".NXR "freopen subroutine".NXR "fdopen subroutine" .NXR "stream" "opening"The.PN fopenroutine opens the file named by.I filename\^and associates a.I stream\^with it.The.PN fopenroutine returns a pointer to the FILE structure associated withthe.IR stream ..PPThe.I filename\^points to a character string that containsthe name of the file to be opened..PPThe.I type\^is a character string having one of the following values:.RS 3.TP 10"r"Open for reading.ns.TP 10"w"Truncate or create for writing.ns.TP 10"a"Append; open for writing at endof file, or create for writing.ns.TP 10"A"Append with no overwrite; open for writingat end-of-file, or create for writing.ns.TP 10"r+"Open for reading and writing.ns.TP 10"w+"Truncate or create for reading and writing.ns.TP 10"a+"Append; open or create for reading and writing at end-of-file.ns.TP 10"A+"Append with no overwrite, open or create for update at end-of-file.RE.PPThe letter "b" can also follow r, w, or a. In some C implementations, the "b" is needed to indicate a binary file, however, it is not needed in ULTRIX. If "+" is used, the "b" may occur on either side, as in "rb+" or "w+b"..PPThe.PN freopenroutine substitutes the named file in placeof the open.IR stream .The original.I stream\^is closed,regardless of whether the openultimately succeeds.The.PN freopenroutine returns a pointer to the FILEstructure associated with.IR stream ..PPThe.PN freopenroutine is typically used to attach the preopened.I streams\^associated with.BR stdin ,.B stdoutand.BR stderrto other files..PPThe.PN fdopenroutine associates a.I stream\^with a file descriptor.File descriptors are obtained from.PN open ,.PN dup ,.PN creat ,or.MS pipe 2 ,which open files but do not return pointers to a FILE structure .I stream\^.Streams are necessary input for many of the Section 3s library routines.The.I type\^of.I stream\^must agree with the mode of the open file..PPWhen a file is opened for update, both input and output may bedone on the resulting.IR stream .However, output may not be directly followed by input without anintervening.PN fseekor.PN rewind ,and input may not be directly followed by output without anintervening.PN fseek ,.PN rewind ,or an input operation which encounters end-of-file..PPWhen a file is opened for append with no overwrite (that is when type is "A" or"A+"), it is impossible to overwrite informationalready in the file.The.PN fseekroutinemay be used to reposition the file pointer to any positionin the file, but when output is writtento the file, the current file pointer is disregarded.All output is written at the end of the file and causes the filepointer to be repositioned at the end of the output. If two separateprocesses open the same file for append, each process may write freelyto the file without fear of destroying output being written by theother. The output from the two processes will be intermixed in thefile in the order in which it is written..SH Return ValuesThe.PN fopenand .PN freopenroutines return a NULLpointer on failure..SH Environment.NXR "fopen subroutine" "System V and".SS SYSTEM_VWhen your program is compiled using the System V environment,append with no overwriteis specified by using the "a" or "a+" type string, and the"A" and "A+" type strings are not allowed..SS POSIXIn the POSIX environment, the "a" and "a+" strings, and the"A" and "A+" strings specify append with no overwrite..SH See Alsocreat(2), dup(2), open(2), pipe(2), fclose(3s), fseek(3s).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -