File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program.
Need of File Handling in C
So far, the operations in C program are done on a prompt/terminal in which the data is only stored in the temporary memory (RAM). This data is deleted when the program is closed. But in the software industry, most programs are written to store the information fetched from the program. The use of file handling is exactly what the situation calls for.
File handling allows us to read and write data on files stored in the secondary memory such as hard disk from our C program.
C File Operations
C language provides the following different operations that we can perform on a file from our C program:
- Creating a new file.
- Opening an existing file.
- Reading from file.
- Writing to a file.
- Moving to a specific location in a file.
- Closing a file.
Components in C File Handling
Before we move on to the file handling, we need to understand a few concepts that are essential in file handling.
File
A file is a container of data. It can be classified into two types based on the way the file stores the data. They are as follows:

- Text Files: A text file contains data in the form of ASCII characters and is generally used to store a stream of characters.
- Each line in a text file ends with a new line character (‘\n’).
- It can be read or written by any text editor.
- They are generally stored with .txt file extension.
- Text files can also be used to store the source code.
- Binary Files: A binary file contains data in binary form (i.e. 0’s and 1’s) instead of ASCII characters. They contain data that is stored in a similar manner to how it is stored in the main memory.
- The binary files can be created only from within a program and their contents can only be read by a program.
- More secure as they are not easily readable.
- They are generally stored with .bin file extension.
File Pointer
A file pointer is a reference to a particular position in the opened file. It is used in file handling to perform all file operations such as read, write, close, etc. We use the FILE macro to declare the file pointer variable. The FILE macro is defined inside <stdio.h> header file.
C
File Pointer is used in almost all the file operations in C.
File Operation Functions
The file operations are performed by using the functions provided as the part of file handling API of C language. Following is the list of commonly used functions:

Open a File in C
For opening a file in C, the fopen() function is used with the filename or file path along with the required access modes.
Syntax:
C
FILE* fopen(*file_name, *access_mode);
Parameters
- file_name: name of the file when present in the same directory as the source file. Otherwise, full path.
- access_mode: Specifies for what operation the file is being opened.
Return Value
- If the file is opened successfully, returns a file pointer to it.
- If the file is not opened, then returns NULL.
File Opening Modes
File opening modes or access modes specify the allowed operations on the file to be opened. They are passed as an argument to the fopen() function. Some of the commonly used file access modes are listed below:
Opening Modes | Description |
---|
r | Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the first character in it. If the file cannot be opened fopen( ) returns NULL. |
rb | Open for reading in binary mode. If the file does not exist, fopen( ) returns NULL. |
w | Open for writing in text mode. If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open the file. |
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. |
a | Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the last character in it. It opens only in the append mode. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open the file. |
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. |
r+ | Searches file. It is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the first character in it. Returns NULL, if unable to open the file. |
rb+ | Open for both reading and writing in binary mode. If the file does not exist, fopen( ) returns NULL. |
w+ | Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist a new file is created. Returns NULL, if unable to open the file. |
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. |
a+ | Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the last character in it. It opens the file in both reading and append mode. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open the file. |
ab+ | Open for both reading and appending in binary mode. If the file does not exist, it will be created. |
As given above, if you want to perform operations on a binary file, then you have to append ‘b’ at the last. For example, instead of “w”, you have to use “wb”, instead of “a+” you have to use “a+b”.
Example:
C
//Driver Code Starts{
#include <stdio.h>
#include <stdlib.h>
int main() {
//Driver Code Ends }
// File pointer to store the
// value returned by fopen
FILE* fptr;
// Opening the file in read mode
fptr = fopen("filename.txt", "r");
// checking if the file is
// opened successfully
if (fptr == NULL) {
printf("The file is not opened.");
}
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
OutputThe file is not opened.
The file is not opened because it does not exist in the source directory. But the fopen() function is also capable of creating a file if it does not exist.
Note: If is essential to check for NULL values that might be returned by the fopen() function to avoid any errors.
Create a File in C
The fopen() function can not only open a file but also can create a file if it does not exist already. For that, we have to use the modes that allow the creation of a file if not found such as w, w+, wb, wb+, a, a+, ab, and ab+.
Example:
C
//Driver Code Starts{
#include <stdio.h>
#include <stdlib.h>
int main() {
//Driver Code Ends }
// File pointer
FILE* fptr;
// Creating file using fopen()
// with access mode "w"
fptr = fopen("file.txt", "w");
// checking if the file is created
if (fptr == NULL)
printf("The file is not opened.");
else
printf("The file is created Successfully.");
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
OutputThe file is created Successfully.

File Created
Write to a File
The file write operations can be performed by the functions fprintf() and fputs(). C programming also provides some other functions that can be used to write data to a file such as:
Function | Description |
---|
fprintf() | Similar to printf(), this function uses formatted string and variable arguments list to print output to the file. |
fputs() | Prints the whole line in the file and a newline at the end. |
fputc() | Prints a single character into the file. |
fputw() | Prints a number to the file. |
fwrite() | This function writes the specified number of bytes to the binary file. |
Example:
C
//Driver Code Starts{
#include <stdio.h>
#include <stdlib.h>
//Driver Code Ends }
int main() {
//Driver Code Starts{
// File pointer
FILE* fptr;
// Get the data to be written in file
char data[50] = "GeeksforGeeks-A Computer "
"Science Portal for Geeks";
// Creating file using fopen()
// with access mode "w"
fptr = fopen("file.txt", "w");
// Checking if the file is created
if (fptr == NULL)
printf("The file is not opened.");
else{
printf("The file is now opened.
");
fputs(data, fptr);
fputs("
", fptr);
// Closing the file using fclose()
fclose(fptr);
printf("Data successfully written in file "
"file.txt
");
printf("The file is now closed.");
}
return 0;
}
//Driver Code Ends }
OutputThe file is now opened.
Data successfully written in file file.txt
The file is now closed.

Reading From a File
The file read operation in C can be performed using functions fscanf() or fgets(). Both the functions performed the same operations as that of scanf() and gets but with an additional parameter, the file pointer. There are also other functions we can use to read from a file. Such functions are listed below:
Function | Description |
---|
fscanf() | Use formatted string and variable arguments list to take input from a file. |
fgets() | Input the whole line from the file. |
fgetc() | Reads a single character from the file. |
fgetw() | Reads a number from a file. |
fread() | Reads the specified bytes of data from a binary file. |
Example:
C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>
int main() {
FILE* fptr;
//Driver Code Ends }
// Declare the character array
// for the data to be read from file
char data[50];
fptr = fopen("file.txt", "r");
if (fptr == NULL) {
printf("file.txt file failed to open.");
}
else {
printf("The file is now opened.
");
// Read the data from the file
// using fgets() method
while (fgets(data, 50, fptr)
!= NULL) {
// Print the data
printf("%s", data);
}
// Closing the file using fclose()
fclose(fptr);
}
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
Output
The file is now opened.
GeeksforGeeks-A Computer Science Portal for Geeks
The getc() and some other file reading functions return EOF (End Of File) when they reach the end of the file while reading. EOF indicates the end of the file, and its value is implementation-defined. Reading more after EOF results in undefined error so, it is always recommended to check for EOF while reading a file.
Note: One thing to note here is that after reading a particular part of the file, the file pointer will be automatically moved to the end of the last read character.
Closing a File
The fclose() function is used to close the file. After successful file operations, you must always close a file to remove it from the memory.
Syntax:
C
Move File Pointer
File pointer generally points to the position according to the mode or last read/write operation. We can manually move this pointer to any position in the file using fseek() function.
Syntax:
C
fseek(fptr, offset, pos);
where, pos is the position from where offset is counted and offset is the number of positions to shift from pos (it can be negative or positive).
Example:
While writing to a file opened in rw+ mode, the file pointer moves to the end of the file. In case where we want to replace a word, then first we have to move the file pointer to the position where that word starts.
C
#include <stdio.h>
#include <stdlib.h>
int main() {
// File pointer
FILE* fptr;
// Get the data to be written in file
char data[50] = "GeeksforGeeks-A Computer "
"Science Portal for Geeks";
// Creating file using fopen()
// with access mode "w"
fptr = fopen("file.txt", "w");
// Checking if the file is created
if (fptr == NULL)
printf("The file is not opened.");
else{
printf("The file is now opened.\n");
fputs(data, fptr);
fputs("\n", fptr);
fseek(fptr, -6, SEEK_END);
fputs("GeeksforGeeks", fptr);
// Closing the file using fclose()
fclose(fptr);
printf("Data successfully written in file "
"file.txt\n");
printf("The file is now closed.");
}
return 0;
}
OutputThe file is now opened.
Data successfully written in file file.txt
The file is now closed.
Now, imagine you want to read this file after writing. We can use fseek() here too, but these is one more function specifically for this purpose which is rewind().
Read and Write in a Binary File
Till now, we have only discussed text file operations. The operations on a binary file are similar to text file operations with little difference.
Opening a Binary File
To open a file in binary mode, we use the rb, rb+, ab, ab+, wb, and wb+ access mode in the fopen() function. We also use the .bin file extension in the binary filename.
Example:
C
fptr = fopen("filename.bin", "rb");
Write to a Binary File
We use fwrite() function to write data to a binary file. The data is written to the binary file in the form of bits (0’s and 1’s).
Syntax of fwrite()
C
fwrite(ptr, size, nmemb, file_pointer);
Parameters:
- ptr: pointer to the block of memory to be written.
- size: size of each element to be written (in bytes).
- nmemb: number of elements.
- file_pointer: FILE pointer to the output file stream.
Return Value:
- Number of objects written.
Example: Program to write to a Binary file using fwrite()
C
//Driver Code Starts{
#include <stdio.h>
#include <stdlib.h>
//Driver Code Ends }
struct threeNum {
int n1, n2, n3;
};
int main() {
int n =1 ;
// Structure variable declared.
struct threeNum num;
FILE* fptr;
fptr = fopen("binaryFile.bin", "wb");
int flag = 0;
num.n1 = n;
num.n2 = 5 * n;
num.n3 = 5 * n + 1;
// Write the Structure data
// to binary file.
flag = fwrite(&num, sizeof(struct threeNum), 1,
fptr);
// Checking if the data is written.
if (!flag)
printf("Write Operation Failure");
else
printf("Write Operation Successful");
fclose(fptr);
//Driver Code Starts{
return 0;
}
//Driver Code Ends }
OutputWrite Operation Successful

Data Written in Binary File
Reading from Binary File
The fread() function can be used to read data from a binary file in C. The data is read from the file in the same form as it is stored i.e. binary form.
Syntax:
C
fread(ptr, size, nmemb, file_pointer);
Parameters:
- ptr: pointer to the block of memory to read.
- size: the size of each element to read (in bytes).
- nmemb: number of elements.
- file_pointer: FILE pointer to the input file stream.
Return Value:
Example: Program to Read from a binary file using fread()
C
#include <stdio.h>
#include <stdlib.h>
// Structure that store
// binary file data
struct threeNum {
int n1, n2, n3;
};
int main() {
int n;
struct threeNum num;
FILE* fptr;
fptr = fopen("binaryFile.bin", "rb");
// Read the data from binary
// file and print that data
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("n1: %d\tn2: %d\tn3: %d\n", num.n1, num.n2,
num.n3);
fclose(fptr);
return 0;
}
Output
n1: 1 n2: 5 n3: 6
More Functions for C File Operations
The following table lists some more functions that can be used to perform file operations or assist in performing them.
Functions | Description |
---|
fopen() | It is used to create a file or to open a file. |
fclose() | It is used to close a file. |
fgets() | It is used to read a file. |
fprintf() | It is used to write blocks of data into a file. |
fscanf() | It is used to read blocks of data from a file. |
getc() | It is used to read a single character to a file. |
putc() | It is used to write a single character to a file. |
fseek() | It is used to set the position of a file pointer to a mentioned location. |
ftell() | It is used to return the current position of a file pointer. |
rewind() | It is used to set the file pointer to the beginning of a file. |
putw() | It is used to write an integer to a file. |
getw() | It is used to read an integer from a file. |
Similar Reads
Basics of File Handling in C
File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program. Need of File Ha
13 min read
C fopen() Function
In C, the fopen() function is used to open a file in the specified mode. The function returns a file pointer (FILE *) which is used to perform further operations on the file, such as reading from or writing to it. If the file exists then the fopen() function opens the particular file else a new file
5 min read
EOF, getc() and feof() in C
In this article, we will discuss the EOF, getc() function and feof() function in C. What is EOF?In C, EOF is a constant macro defined in the <stdlib.h> header file that is used to denote the end of the file in C file handling. It is used by various file reading functions such as fread(), gets(
3 min read
fgets() in C
In C, fgets() is a built-in function defined in <stdio.h> header file. It reads the given number of characters of a line from the input stream and stores it into the specified string. This function stops reading the characters when it encounters a newline character, has read the given number o
4 min read
fprintf() in C
fprintf is used to print content in file instead of stdout console. int fprintf(FILE *fptr, const char *str, ...); Example: C/C++ Code // C Program for the above approach #include<stdio.h> int main() { int i, n=2; char str[50]; //open file sample.txt in write mode FILE *fptr = fopen("samp
1 min read
scanf() and fscanf() in C
In C language, scanf() function is used to read formatted input from stdin. It returns the whole number of characters written in it otherwise, returns a negative value. Syntax: int scanf(const char *characters_set)Time Complexity: O(n) Auxiliary Space: O(n) where n is the length of input. Many of us
4 min read
C fread() Function
The C fread() is a standard library function used to read the given amount of data from a file stream. Defined inside <stdio.h>, the fread() function reads the given number of elements of specific size from the file stream and stores it in the buffer memory. The total number of bytes read by f
4 min read
fseek() vs rewind() in C
In C, the functions fseek() and rewind() helps in manipulating the position of the pointer in the opened file but serve different purposes and have distinct working. The below table lists the major differences between fseek() and rewind() in C: Featurefseek()rewind()FunctionalityMoves the file point
2 min read
What is return type of getchar(), fgetc() and getc() ?
In C, getchar(), fgetc(), and getc() all are the functions used for reading characters from input buffer. This buffer is standard input buffer for getchar() and can be any specified file for getc() and fgetc(). In this article, we will learn about the return type of these functions and why it matter
3 min read
Read/Write Structure From/to a File in C
For writing in the file, it is easy to write string or int to file using fprintf and putc, but you might have faced difficulty when writing contents of the struct. fwrite and fread make tasks easier when you want to write and read blocks of data. Writing Structure to a File using fwriteWe can use fw
3 min read