Shell Shell

File Operations

These function prototypes are in <stdio.h>: the program must #include <stdio.h>.

The value EOF (indicates end-of-file) is defined in <stdio.h>. Its value is usually -1, but this should not be relied upon.

The data type FILE is a struct type defined in <stdio.h>. A stream is a pointer-to-FILE, and comprises a buffer plus a file. The streams stdin, stdout and stderr are automatically opened when a program including <stdio.h> is run. Unless redirected, stdin is connected to the keyboard, stdout and stderr to the screen. When program execution ends, all open streams are automatically closed.


FILE *fopen(const char *filename, const char *mode);

This opens the file filename. The mode mode can be "r" for reading, "w" for writing, or "a" for appending. It returns a stream, or NULL if the file cannot be opened.

int fflush(FILE *stream);

Flushing an output stream causes any unwritten data in the buffer to be written. On an input stream, the effect is undefined (but usually clears the buffer). The return value is normally zero, or EOF for a write error.

int fclose(FILE *stream);

This closes stream. For an output stream, any buffered data is first written. For an input stream, any unread data is discarded. It normally returns zero, or EOF for an error.

int remove(const char *filename);

This deletes the file filename. It normally returns zero, or non-zero for an error.

int rename(const char *oldname, const char *newname);

This renames the file oldname to newname. It normally returns zero, or non-zero for an error.

int fprintf(FILE *stream, const char *format, ...);

This acts exactly like printf except that the data is written to stream, rather than stdout. It normally returns the number of characters written, or a negative value for an error.

int fscanf(FILE *stream, const char *format, ...);

This acts exactly like scanf except that the data is read from stream, rather than stdin. It normally returns the number of input items converted and assigned, or EOF for an error.

int fgetc(FILE *stream);

This normally returns the next character read from stream (as an int), or EOF for an error. (The char is converted to an int because EOF is usually -1, while unsigned char is usually from 0 to 255 so could not be used to return EOF.)

char *fgets(char *s, int n, FILE *stream);

This reads a maximum of n - 1 characters into the array s. It stops if a newline ('\n') is found. The newline is included in the array. The array is terminated by the null character '\0'. It returns s (a pointer-to-char), or NULL for an error.

int fputc(int c, FILE *stream);

This writes the character c (converted to unsigned char) to stream. It normally returns c, or EOF for an error.

int fputs(const char *s, FILE *stream);

This writes string s to stream. (Note: unlike puts, fputs does not add a newline '\n' to the end of s.) It normally returns a non-negative value, or EOF for an error.

void rewind(FILE *stream);

This "rewinds" stream back to the start of the file. It does not return a value.

int feof(FILE *stream);

This normally returns zero, or non-zero if the end-of-file marker for stream has been reached.
David C. Hamill
D.Hamill@surrey.ac.uk