Skip to content
Related Articles
Open in App
Not now

Related Articles

Basics of File Handling in C

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 01 Feb, 2023
Improve Article
Save Article

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 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: 

  1. Creation of a new file (fopen() with attributes as “a” or “a+” or “w” or “w+”)
  2. Opening an existing file (fopen())
  3. Reading from file (fscanf() or fgets())
  4. Writing to a file (fprintf() or fputs())
  5. Moving to a specific location in a file (fseek(), rewind())
  6. Closing a file (fclose())

The text in the brackets denotes the functions used for performing those operations.

Why do we need File Handling in C?

The output of a C program is generally deleted when the program is closed. Sometimes, we need to store that output for purposes like data analysis, result presentation, comparison of output for different conditions,  etc. The use of file handling is exactly what the situation calls for.

In order to understand why file handling makes programming easier, let us look at a few reasons:

  • Reusability: The file-handling process keeps track of the information created after the program has been run.
  • Portability: Without losing any data files can be transferred to another in the computer system. The risk of flawed coding is minimized with this feature.
  • Efficient: A large amount of input may be required for some programs. File handling allows you to easily access a part of a code using individual commands which saves a lot of time and reduces the chance of errors.
  • Storage Capacity: Files allow you to store data without having to worry about storing everything simultaneously in a program.

Types of Files in C

Generally, a text file contains alphabets, digits, and special characters or symbols, while a binary file contains bytes or a compiled version of the text. It is important to recognize two types of files when dealing with files:

  • Text Files
  • Binary Files

Text Files: Text files contain data in the form of ASCII characters and are generally used to store a stream of characters. Each line in a text file ends with a new line character (‘/n’). Text files are used to store the source code.

Binary Files: Binary files contain data that is stored in a similar manner to how it is stored in the main memory. Instead of ASCII characters, it is stored in binary format. The binary files can be created only from within a program and their contents can only be read by a program.

Functions in File Operations:

file handling in c

 

Opening or Creating a File

For opening a file, fopen() function is used with the required access modes. Some of the commonly used file access modes are mentioned below.

File opening modes in C: 

Opening ModesDescription
rSearches 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.
wSearches 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.
wbOpen for writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
aSearches 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. 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. 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”. For performing the operations on the file, a special pointer called File pointer is used which is declared as:

FILE *filePointer; 

So, the file can be opened as 

filePointer = fopen(“fileName.txt”, “w”)

The second parameter can be changed to contain all the attributes listed in the above table.

Reading From a File

The file read operations 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. So, it depends on you if you want to read the file line by line or character by character.
And the code snippet for reading a file is as: 
 

FILE * filePointer; 

filePointer = fopen(“fileName.txt”, “r”);

fscanf(filePointer, "%s %s %s %d", str1, str2, str3, &year);

Writing to a File

The file write operations can be performed by the functions fprintf and fputs with similarities to read operations. The snippet for writing to a file is as:

FILE *filePointer ; 

filePointer = fopen(“fileName.txt”, “w”);

fprintf(filePointer, "%s %s %s %d", "We", "are", "in", 2012);

Closing a File

After every successful file operation, you must always close a file. For closing a file, you have to use fclose() function. The snippet for closing a file is given as:

FILE *filePointer ; 

filePointer= fopen(“fileName.txt”, “w”);

---------- Some file Operations -------

fclose(filePointer)

Example 1: Program to Open a File, Write in it, And Close the File

C




// C program to Open a File,
// Write in it, And Close the File
 
# include <stdio.h>
# include <string.h>
 
int main( )
{
 
    // Declare the file pointer
    FILE *filePointer ;
     
    // Get the data to be written in file
    char dataToBeWritten[50]
        = "GeeksforGeeks-A Computer Science Portal for Geeks";
 
    // Open the existing file GfgTest.c using fopen()
    // in write mode using "w" attribute
    filePointer = fopen("GfgTest.c", "w") ;
     
    // Check if this filePointer is null
    // which maybe if the file does not exist
    if ( filePointer == NULL )
    {
        printf( "GfgTest.c file failed to open." ) ;
    }
    else
    {
         
        printf("The file is now opened.\n") ;
         
        // Write the dataToBeWritten into the file
        if ( strlen ( dataToBeWritten ) > 0 )
        {
             
            // writing in the file using fputs()
            fputs(dataToBeWritten, filePointer) ;
            fputs("\n", filePointer) ;
        }
         
        // Closing the file using fclose()
        fclose(filePointer) ;
         
        printf("Data successfully written in file GfgTest.c\n");
        printf("The file is now closed.") ;
    }
    return 0;       
}

Output:

The file is now opened.
Data successfully written in file GfgTest.c
The file is now closed.

This program will create a file named GfgTest.c in the same directory as the source file which will contain the following text: “GeeksforGeeks-A Computer Science Portal for Geeks”.

Example 2: Program to Open a File, Read from it, And Close the File

C




// C program to Open a File,
// Read from it, And Close the File
 
# include <stdio.h>
# include <string.h>
 
int main( )
{
 
    // Declare the file pointer
    FILE *filePointer ;
     
    // Declare the variable for the data to be read from file
    char dataToBeRead[50];
 
    // Open the existing file GfgTest.c using fopen()
    // in read mode using "r" attribute
    filePointer = fopen("GfgTest.c", "r") ;
     
    // Check if this filePointer is null
    // which maybe if the file does not exist
    if ( filePointer == NULL )
    {
        printf( "GfgTest.c file failed to open." ) ;
    }
    else
    {
         
        printf("The file is now opened.\n") ;
         
        // Read the dataToBeRead from the file
        // using fgets() method
        while( fgets ( dataToBeRead, 50, filePointer ) != NULL )
        {
         
            // Print the dataToBeRead
            printf( "%s" , dataToBeRead ) ;
        }
         
        // Closing the file using fclose()
        fclose(filePointer) ;
         
        printf("Data successfully read from file GfgTest.c\n");
        printf("The file is now closed.") ;
    }
    return 0;       
}

Output:

The file is now opened.
GeeksforGeeks-A Computer Science Portal for Geeks
Data successfully read from file GfgTest.c
The file is now closed.

This program reads the text from the file named GfgTest.c which we created in the previous example and prints it in the console.

Write to a Binary File

Example 3: Program to write to a Binary file using fwrite()
 

C




// C program to write to a Binary file using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct threeNum {
    int n1, n2, n3;
};
int main()
{
    int n;
    // Structure variable declared here.
    struct threeNum num;
    FILE* fptr;
    if ((fptr = fopen("C:\\program.bin", "wb")) == NULL) {
        printf("Error! opening file");
        // If file pointer will return NULL
        // Program will exit.
        exit(1);
    }
    // else it will return a pointer to the file.
    for (n = 1; n < 5; ++n) {
        num.n1 = n;
        num.n2 = 5 * n;
        num.n3 = 5 * n + 1;
        fwrite(&num, sizeof(struct threeNum), 1, fptr);
    }
    fclose(fptr);
    return 0;
}

Reading from Binary File

Example 4: Program to Read from a binary file using fread()

C




// C Program to Read from a binary file using fread()
#include <stdio.h>
#include <stdlib.h>
struct threeNum {
    int n1, n2, n3;
};
int main()
{
    int n;
    struct threeNum num;
    FILE* fptr;
    if ((fptr = fopen("C:\\program.bin", "rb")) == NULL) {
        printf("Error! opening file");
        // If file pointer will return NULL
        // Program will exit.
        exit(1);
    }
    // else it will return a pointer to the file.
    for (n = 1; n < 5; ++n) {
        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
n1: 2   n2: 10  n3: 11
n1: 3   n2: 15  n3: 16
n1: 4   n2: 20  n3: 21

Accessing Data using  fseek()

If we have multiple records inside a file and need to access a particular record that is at a specific position, so we need to loop through all the records before it to get the record. Doing this will waste a lot of memory and operational time. To reduce memory consumption and operational time we can use fseek() which provides an easier way to get to the required data. fseek() function in C seeks the cursor to the given record in the file.

Syntax for fseek():

int fseek(FILE *ptr, long int offset, int pos);

C




// C Program  to demonstrate the use of fseek() in C
#include <stdio.h>
 
int main()
{
    FILE* fp;
    fp = fopen("test.txt", "r");
 
    // Moving pointer to end
    fseek(fp, 0, SEEK_END);
 
    // Printing position of pointer
    printf("%ld", ftell(fp));
 
    return 0;
}

Output:

81

If you want to read more about fseek() then you can refer to fseek() in C/C++ with an example.

More Functions for File Handling in C

FunctionsDescription
fopen()to create a file or to open a file.
fclose()to close a file.
fgets()to read a file.
fprintf()to write blocks of data into a file.
fscanf()to read blocks of data from a file.
getc()to read a single character to a file.
putc()to write a single character to a file.
fseek()to set the position of a file pointer to a mentioned location.
ftell()to return the current position of a file pointer.
rewind()to set the file pointer to the beginning of a file.
putw()to write an integer to a file.
getw()to read an integer from a file.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!