Site hosted by Angelfire.com: Build your free website today!

Table of Contents Previous Next


1.12.1 <stdio.h>: File Operations

The following functions deal with operations on files.  The type size_t is the unsigned integral type produced by the sizeof operator.


FILE *fopen(const char *filename, const char *mode)
fopen opens the named file.  Legal values for mode include

"r" open text file for reading
"w" create text file for writing
"a" append; open or create text file for writing at end of file
"r+" open text file for update (i.e. reading and writing)
"w+" create text file for update; discard previous contents if any
"a+" append; open or create text file for update, writing at end

Update mode permits reading and writing the same file; fflush or a file positioning function must be called between a read and a write or vice versa.  If the mode includes b after the initial letter, as in "rb" or "w+b", that indicates a binary file.  File names are limited to FILENAME_MAX characters.  At most FOPEN_MAX files may be open at once.

Returns
A stream, or NULL if the attempt fails.


char *freopen(const char *filename, const char *mode, FILE *stream);
freopen opens the file with the specified mode and associates the stream with it.  freopen is normally used to change the files associated with stdin, stdout, or stderr.

Returns
freopen returns stream or NULL if an error occurs.


int fflush(FILE *stream)
On an output stream, fflush causes any buffered but unwritten output data to be written; on an input stream, the effect is undefined.  fflush(NULL) flushes all output streams.

Returns
EOF for a write error, and zero otherwise.


int fclose(FILE *stream)
fclose flushes any unwritten data for stream, discards any unread buffered input, frees any automatically allocated buffer, then closes the stream.

Returns
EOF if any errors occurred, and zero otherwise.


int remove(const char *filename)
remove removes the named file, so that a subsequent attempt to open it will fail.

Returns
Non-zero if the attempt to remove the file fails.


int rename(const char *oldname, const char *newname)
rename changes the name of a file.

Returns
rename returns non-zero if the attempt to rename a file fails.


FILE *tmpfile(void)
tmpfile creates a temporary file of mode "wb+" that will be automatically removed when closed or when the program terminates normally.

Returns
tmpfile returns a stream, or NULL if it could not create the file.


char *tmpnam(char s[L_tmpnam])
tmpnam(NULL) creates a string that is not the name of an existing file, and returns a pointer to an internal static array.  tmpnam(s) stores the string in s as well as returning it as the function value; s must have room for at least L_tmpnam characters.  tmpnam generates a different name each time it is called; at most TMP_MAX different names are guaranteed during execution of the program.  Note that tmpnam creates a name, not a file.

Returns
tmpnam returns a pointer to an internal static array containing the string.


int setvbuf(FILE *stream, char *buf, int mode, size_t size)
setvbuf controls buffering for the stream; it must be called before reading, writing, or any other operation.  A mode of _IOFBF causes full buffering, _IOLBF line buffering of text files, and _IONBF no buffering.  If buf is not NULL, it will be used as the buffer; otherwise a buffer will be allocated.  size determines the buffer size.

Returns
setvbuf returns non-zero for any error.


void setbuf(FILE *stream, char *buf)
If buf is NULL, buffering is turned off for the stream.  Otherwise, setbuf is equivalent to void setvbuf(stream, buf, _IOFBF, BUFSIZE).


Table of Contents Previous Next

Last modified: Thu May 18 12:44:43 2000