C File Handling

The random access memory is volatile and its content is lost once the program terminates. In order to persist the data forever, we use files.

A file data stored in a storage device. A-C program can talk to the file by reading content from it and writing content to it.

C files I/O functions handle data on a secondary storage device, such as a hard disk.

C can handle files as Stream-oriented data (Text) files, and System oriented data (Binary) files.

Stream-oriented data filesThe data is stored in the same manner as it appears on the screen. The I/O operations like buffering, data conversions, etc. take place automatically.
System-oriented data filesSystem-oriented data files are more closely associated with the OS and data stored in memory without converting into text format.

Why files are needed?

  • When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program terminates.
  • If you have to enter a large number of data, it will take a lot of time to enter them all.
    However, if you have a file containing all the data, you can easily access the contents of the file using a few commands in C.
  • You can easily move your data from one computer to another without any changes.

File pointer

The “File” is a structure that needs to be created for opening the file. A file pointer is a pointer to this structure of the file.

File pointer is needed for communication between the file and the program.

A file pointer can be created as follows:

FILE *ptr;

File opening modes in C

C offers the programmers to select a mode for opening a file.

Following modes are primarily used in c File I/O

C File Open

Types of Files

There are two types of files:

  1. Text files(.txt, .c)
  2. Binary files(.jpg, .dat)

1. Text files

Text files are the normal .txt files. You can easily create text files using any simple text editors such as Notepad.

When you open those files, you’ll see all the contents within the file as plain text. You can easily edit or delete the contents.

They take minimum effort to maintain, are easily readable, and provide the least security and takes bigger storage space.

2. Binary files

Binary files are mostly the .bin files in your computer.

Instead of storing data in plain text, they store it in the binary form (0’s and 1’s).

They can hold a higher amount of data, are not readable easily, and provides better security than text files.

Reading a file

A file can be opened for reading as follows:

FILE *ptr;
int num;

Let us assume that “programiz.txt” contains an integer

We can read that integer using:

fscanf(ptr,”%d”,&num);                => fscanf is file counterpart of scanf

This will read an integer from the file in the num variable.

Quick Quiz: Modify the program above to check whether the file exists or not before opening the file.

Closing the file

It is very important to close file after read or write. This is achieved using fclose as follows:


This will tell the compiler that we are done working with this file and the associated resources could be freed.

Writing to a file

We can write to a file in a very similar manner as we read the file

FILE *fptr;
int num=432;

fgetc() and fputc()

fgetc and fputc are used to read and write a character from/to a file.

fgetc(ptr);                         => Used to read a character from file

fputc(‘c’,ptr);                     => Used to write character 'c' to the file

EOF: End of File

fgetc returns EOF when all the characters from a file have read. So we can write a check like below to detect the end of file.

ch=fgetc(ptr);	   // When all the content of a file has been read, break the loop

Exercise on File Handling

  1. Write a program to read three integers from a file.
  2. Write a program to generate a multiplication table of a given number in text format. Make sure that the file is readable and well-formatted.
  3. Write a program to read a text file character by character and write its content twice in a separate file.
  4. Take name and salary of two employees as input from the user and write them to a text file in the following format:
        name1, 3300

        name2, 7700
  1. Write a program to modify a file containing an integer to double its value.

       If old value = 2, then new file value = 4

Was this article helpful?

Leave a Comment

Your email address will not be published. Required fields are marked *