354 lines · 7.6 KB · Text
RawDownload
.TH FOPEN 2.SH NAMEfopen, freopen, fdopen, fileno, fclose, sopenr, sopenw, sclose, fflush, setvbuf, setbuf, fgetpos, ftell, fsetpos, fseek, rewind, feof, ferror, clearerr \- standard buffered input/output package.SH SYNOPSIS.B #include <u.h>.br.B #include <stdio.h>.PP.ta \w'\fLFILE 'u.BFILE	*fopen(char *filename, char *mode).PP.BFILE	*freopen(char *filename, char *mode, FILE *f).PP.BFILE	*fdopen(int fd, char *mode).PP.Bint	fileno(FILE *f).PP.BFILE	*sopenr(char *s).PP.BFILE	*sopenw(void).PP.Bchar	*sclose(FILE *f).PP.Bint	fclose(FILE *f).PP.Bint	fflush(FILE *f).PP.Bint	setvbuf(FILE *f, char *buf, int type, long size).PP.Bvoid	setbuf(FILE *f, char *buf).PP.Bint	fgetpos(FILE *f, long *pos).PP.Blong	ftell(FILE *f).PP.Bint	fsetpos(FILE *f, long *pos).PP.Bint	fseek(FILE *f, long offset, int whence).PP.Bvoid	rewind(FILE *f).PP.Bint	feof(FILE *f).PP.Bint	ferror(FILE *f).PP.Bvoid	clearerr(FILE *f).SH DESCRIPTIONThe functions described in this and related pages.RI ( fgetc (2),.IR fprintf (2),.IR fscanf (2),and.IR tmpfile (2))implement theANSI C buffered I/O package with extensions..PPA file with associated buffering is called a.I streamand is declared to be a pointer to a defined type.BR FILE ..IR  Fopen (2)creates certain descriptive data for a streamand returns a pointer to designate the stream in allfurther transactions.There are three normally open streams with constantpointers declared inthe include file and associated with the standard open files:.TP 10n.BR stdinstandard input file.br.ns.TP.B stdoutstandard output file.br.ns.TP.B stderrstandard error file.PPA constant pointer.B NULLdesignates no stream at all..PP.I Fopenopens the file named by.I filenameand associates a stream with it..I Fopenreturns a pointer to be used to identifythe stream in subsequent operations, or.B NULLif the open fails..I Modeis a character string having one of the following values:.nf.ta 8n\fL"r"\fP	open for reading\fL"w"\fP	truncate to zero length or create for writing\fL"a"\fP	append; open or create for writing at end of file\fL"r+"\fP	open for update (reading and writing)\fL"w+"\fP	truncate to zero length or create for update\fL"a+"\fP	append; open or create for update at end of file.fi.PPIn addition, each of the above strings can have a.L bsomewhere after the first character,meaning `binary file', but this implementation makes no distinctionbetween binary and text files..PP.I Fclosecauses the stream pointed to by.I fto be flushed (see below) and does a.I close(see.IR open (2))on the associated file.It frees any automatically allocated buffer..I Fcloseis called automatically on .IR exits (2)for all open streams..PP.I Freopenis like open except that it reuses stream pointer.IR f ..I Freopenfirst attempts to close any file associated with.IR f ;it ignores any errors in that close..PP.I Fdopenassociates a stream with an open Plan 9 file descriptor..PP.I Filenoreturns the number of the Plan 9 file descriptor associated with the stream..PP.I Sopenrassociates a read-only stream with a null-terminated string. .PP.I Sopenwopens a stream for writing.No file descriptor is associated with the stream;instead, all output is written to the stream buffer..PP.I Sclosecloses a stream opened with.I sopenror.IR sopenw .It returns a pointer to the 0 terminated buffer associated with the stream..PPBy default, output to a stream is fully buffered: it is accumulated ina buffer until the buffer is full, and then.I write(see.IR read (2))is used to write the buffer.An exception is standard error, which is line buffered:output is accumulated in a buffer untila newline is written.Input is also fully buffered by default; this means that.IR read (2)is used to fill a buffer as much as it can, and then charactersare taken from that buffer until it empties..I Setvbufchanges the buffering method for file.I faccording to.IR type:either.B _IOFBFfor fully buffered,.B _IOLBFfor line buffered,or.B _IONBFfor unbuffered (each character causes a.I reador.IR write).If.I bufis supplied, it is used as the buffer and.I sizeshould be its size;If.I bufis zero, a buffer of the given size is allocated (except for the unbuffered case) using.IR malloc (2)..PP.I Setbufis an older method for changing buffering.  If.I bufis supplied, it changes to fully buffered with the given buffer, which shouldbe of size.B BUFSIZ(defined in.BR stdio.h ).If.I bufis zero, the buffering method changes to unbuffered..PP.I Fflushflushes the buffer of output stream.IR f ,delivering any unwritten buffered data to the host file..PPThere is a.I file position indicatorassociated with each stream.It starts out pointing at the first character (unless the file is openedwith append mode, in which case the indicator is always ignored).The file position indicator is maintained by the reading and writingfunctions described in.IR fgetc (2)..PP.I Fgetposstores the current value of the file position indicator for stream.I fin the object pointed to by.IR pos .It returns zero on success, nonzero otherwise..I Ftellreturns the current value of the file position indicator.The file position indicator is tobe used only as an argument to.IR fseek..PP.I Fsetpossets the file position indicator for stream.I fto the value of the object pointed to by.IR pos ,which shall be a value returned by an earlier call to.I fgetposon the same stream.It returns zero on success, nonzero otherwise..I Fseekobtains a new position, measured in characters from the beginningof the file, by adding.I offsetto the position specified by.IR whence :the beginning of the file if.I whenceis.BR SEEK_SET ;the current value of the file position indicator for.BR SEEK_CUR ;and the end-of-file for.BR SEEK_END ..I Rewindsets the file position indicator to the beginning of the file..PPAn integer constant.B EOFis returnedupon end of file or error by integer-valued functions thatdeal with streams..I Feofreturns non-zero if and only if.I fis at its end of file..PP.I Ferrorreturns non-zero if and only if.I fis in the error state.  It can get into the error stateif a system call failed on the associated fileor a memory allocation failed..I Clearerrtakes a stream out of the error state..SH SOURCE.B /sys/src/libstdio.SH "SEE ALSO".IR fprintf (2),.IR fscanf (2),.IR fgetc (2).br.IR open (2), .IR read (2).SH DIAGNOSTICSThe value.B EOFis returned uniformly to indicate that a.B FILEpointer has not been initialized with.IR fopen ,input (output) has been attempted on an output (input) stream,or a.B FILEpointer designates corrupt or otherwise unintelligible.B FILEdata..brSome of these functions set.IR errstr ..SH BUGSBuffering of output can prevent output datafrom being seen until long after it is computed \- perhapsnever, as when an abort occurs between buffer filling and flushing..brBuffering of input can cause a process to consumemore input than it actually uses.This can cause trouble across.IR exec (2)..brBuffering may delay the receipt of a write error until a subsequent.I stdiowriting, seeking, or file-closing call..brANSI says that a file can be fully buffered only ifthe file is not attached to an interactive device.In Plan 9 all are fully buffered except standard error..PP.IR Fdopen ,.IR fileno , .IR sopenr , .IR sopenw ,and.I scloseare not ANSI Stdio functions..PPStdio offers no support for runes or.SM UTFcharacters.Unless external compatibility is necessary, use.IR bio (2),which supports.SM UTFand is smaller, faster, and simpler than Stdio.