Basics of File Handling in C
So far the operations using the C program are done on a prompt/terminal which is not stored anywhere. But in the software industry, most the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. Different operations that can be performed on a file are:
No. | Function | Description |
---|---|---|
1 | fopen() | opens a new or existing file |
2 | fprintf() | write data into the file |
3 | fscanf() | reads data from the file |
4 | fputc() | writes a character into the file |
5 | fgetc() | reads a character from the file |
6 | fclose() | closes the file |
7 | fseek() | sets the file pointer to the given position |
8 | fputw() | writes an integer to file |
9 | fgetw() | reads an integer from the file |
10 | ftell() | returns current position |
11 | rewind() | sets the file pointer to the beginning of the file |

File opening modes in C:
Mode | Description |
---|---|
r | Opens an existing text file for reading purposes. |
w | Opens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file. |
a | Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content. |
r+ | Opens a text file for both reading and writing. |
w+ | Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist. |
a+ | Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended. |
rb | Open for reading in binary mode. If the file does not exist, fopen( ) returns NULL. |
wb | Open for writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
ab | Open for append in binary mode. Data is added to the end of the file. If the file does not exist, it will be created. |
rb+ | Open for both reading and writing in binary mode. If the file does not exist, fopen( ) returns NULL. |
wb+ | Open for both reading and writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
ab+ | Open for both reading and appending in binary mode. If the file does not exist, it will be created. |
Please find codes of 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, and MISC on the below page:
Top 100+ C Programming codes – KLE Technological University