C File I/O

C File Input and Output


open() Opens a file
fclose() Closes a file
putc() Writes a character to a file
fputc() Same as putc()
getc() Reads a character from a file
fgetc() Same as getc()
fgets() Reads a string from a file
fputs() Writes a string to a file
fseek() Seeks to a specified byte in a file
ftell() Returns the current file position
fprintf() Is to a file what printf() is to the console
fscanf() Is to a file what scanf() is to the console
feof() Returns true if end-of-file is reached
ferror() Returns true if an error has occurred
rewind() Resets the file position indicator to the beginning of the file
remove() Erases a file
fflush() Flushes a file

The File Pointer

The file pointer is simply the common thread that unites the C I/O system. A file pointer is a pointer to a structure of type FILE. it points to the information that defines various things about the file, including its name, status, and the current position of the file. Here is the statement to use the file pointers in your C program:

FILE *fp;

Opening a File in C

To open a file in C, use fopen() function. The fopen() function opens a stream for use and links a file with that stream. And then it returns the file pointer associated with that file. Here is the prototype of the function fopen()

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