File Handling in C – Part 1

In the last tutorial I told you about the basic console input and output functions. Today I will tell you about the second type of input output functions i.e. file input output functions. We will discuss about concept of file handling in C. So without making any delay lets get started.

File Handling in C - Part 1
File Handling in C – Image Source

File Handling in C

Need of file input output functions

During serious C programming we often want to access huge amount of data. Such data cannot be displayed on the screen by once. We cannot store that data into memory due to its large size. Also it would be inappropriate to store such a large data in memory, due to its volatile nature. So either we have to store that data through keyboard again or regenerate it programmatically. Obviously both the methods are quite inefficient. In such cases we store data in a file. To manipulating those files we use file input output functions.

Data organization in a file

Data is always stored in binary form. However its technique in storing that binary data varies from one operating system to another. But as a programmer we don’t have to bother about that. Compiler vendors do this task by writing the proper library functions according to targeted operating system.

Operations on File

Given below are the operations carried out on file through C programming.

1. Creating file
2. Opening an existing file
3. Reading from a file
4. Writing to a file
5. Moving to a specific location in file
6. Closing a file

Now its time to make a program that will display the content of a file on screen.

#include<stdio.h>

void main() 
{
 FILE *fp;
 char ch;
 fp=fopen("demo.txt","r"); //opening file in read mode
 
 while(ch!=EOF)  //loop will continue till end of file
 {
  ch=fgetc(fp); //reading character from file one by one
  printf("%c",ch);
 }
 
 fclose(fp);  //closing the file
}

Output
Above program will display the contents of the file demo.txt on screen.

Explanation
1. In the beginning of the program I have declared a structure pointer fp. But wait, where is the structure FILE? Well it is already defined in the header file stdio.h. We need to use structure variable fp that will point to the current position in the file.

2. After that I have created a character variable ch that will store the character that we read from file.

3. Now I have written a statement fp=fopen(“demo.txt”,”r”). fopen() is the function which is used to open the file. Inside that function I have passed two arguments. One with the name of the file and “r” to specify that file is opened in read mode. This function does three tasks which are as follows

  • It search for the desired file on the disk.
  • After that it loads the content of the file in memory which is called buffer.
  • Then it points the structure variable fp to the first character of file.

Note: Here usage of buffer is quite important as it would be inefficient to load every character from the file every time. It will also take a lot of time. Buffer is also used while writing content in some file. Make sure that the file is already created on this disk.

4. After that I have used while loop and inside that I have written one statement ch=fgetc(fp) which is used to fetch one character from the file to the variable ch.

5. If the content of the file comes an end then value of ch will be EOF and the while loop will end.

6. At the end of the loop I have used printf() function to print that character on the screen.

7. In last I have closed the file using fclose() function.

File handling in C is very important concept and difficult too. Many people find difficulty in learning this concept. So I would recommend you to read this tutorial properly and practice the program. If you have any queries then fell free to ask it in comment section.

Leave a Comment

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