Coding Reference

C
C Libraries
   STDIO

STDIO

<stdio.h>

File Operations

FILE *fopen(const char *filname, const char *mode)
Returns a stream to the opened file or NULL.
Modes:
"r"	open text file for reading
"w"	open text file for writing - discard previous file
"a"	open text file for writing - append text at end of previous file
"r+"	open text file for reading and writing
"w+"	open text file for reading and writing - discard previous file
"a+"	open text file for reading and writing - append text at end of previous file

"b"	"b" may be added to any mode to indicate a binary file instead of text, ie. "w+b"

FILE *freopen(const char *filename, const char *mode, FILE *stream)
Returns a stream to the opened file or NULL and associates the given stream with it.

int fflush(FILE *stream)
Causes any buffered but unwritten data to be written from the output stream.
Returns 0 for success or EOF for a write error.
Passing in NULL causes all streams to be flushed.

int fclose(FILE *stream)
Flushes any unwritten data for the stream.
Discards any unread buffered input.
Frees any automatically allocated buffer.
Closes the stream
Returns 0 for success or EOF if errors occured.

int remove(const char *filename)
Removes the file.
Returns 0 for success, nonzero for failure.

int rename(const char *oldname, const char *newname)
Renames the file.
Returns 0 for success, nonzero for failure.

FILE *tmpfile()
Creates a temporary file.
Returns the stream or NULL.
The temporary file will be removed when closed or when the program terminates normally.

char *tmpnam(char s[L_tmpnam])
Creates a temporary name for a file that is not the name of an existing file.
Returns a string (static char array).
If s[L_tmpnam] is not NULL then it also places the temporary name into the array.
There must be room for L_tmpnam letters.
At most there will be TMP_MAX different names.

int setvbuf(FILE *stream, char *buf, int mode size_t size)
Controls buffering for a stream and must be called after opening but before any other operation on a stream.
Returns 0 for success, non-zero for any error.
Modes
_IOFBF: full buffering
_IOLBF: line buffering of text files
_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.

Formatted Output

Formatted Input

General Input and Output
Ad Placement
contact me | terms of use | copyright