file handling in c language
C Programming gate study material for cse

File Handling in C Language

File Handling in C Language

File handling in c language plays a vital role in managing the data. Here the term managing the data means several tasks to be performed in data management.

File handling in c language is used to update, create, read, and delete a file stored on the local file system on your computer. We use several built-in functions defined in c programming to do these tasks.

This file handling in c language tutorial is written for the computer science student to make them aware of different concepts of file handling in c language.

Frequently Asked Questions

By the end of this tutorial, the student will able able to answer the following questions

  • What is a file?
  • What is file handling in c language?
  • Knowledge of different functions used in file handling?
  • What is a file pointer?
  • Syntax of additional file handling functions in c language.

This tutorial covers the concepts of file handling in language. Several file handling functions in c and their use are also explained in this tutorial. This file handling in c tutorial also covers the concepts of file pointer in c, c program to read and write to a file, opening, and closing a file.

What is a File?

In order to understand the concepts of file handling in C Language at first, we have to understand what the file is? In the context of a computer system file is a collection of data or sequences of bytes stored on the hard disk.

In general, the file is a collection of logically related records. Each file has several attributes.

What is File Pointer?

In file handling, C language uses a structure pointer of type FILE to communicates with files. This pointer is a file pointer in c. This type is defined within stdio.h and written as FILE *.

If we have to declare a file pointer called output_file then it is expressed in a statement as follow –

                                                 FILE *output_file;

Function for File Handling in C Language

Several built-in function used in file handling is given below with their description.
S.No Function Syntax       Objective of Function
1 fopen() To open a new or existing file
2 fprintf() To write data into the file
3 fscanf() To reads data from the file
4 fputc() To writes a character into the file
5 fgetc() To reads a character from a file
6 fclose() To closes the file
7 fseek() To sets the file pointer to a given position
8 fputw() To writes an integer to file
9 fgetw() To reads an integer from a file
10 ftell() It returns the current position
11 rewind() This function sets the file pointer to the beginning of the file

How to Open a File

In order to read or access from file from a c program at first program must open that file before it can access it. This can happen using the fopen() function; this function returns the necessary file pointer.

There may be a situation. If the file does not open correctly due to some causes, then it will return a NULL value.

Syntax to open a file is as follow –

if ((output_file = fopen(“output_file”, “w”)) == NULL)

fprintf(stderr, “Cannot open %sn”, “output_file”);

open() accepts two arguments; both are strings, the first argument is the name of the file to which we want to open, and the second argument is an access character, which represents the access mode.

It may be any one among the following.

“r” – we use it only for reading purposes. When the file is opened correctly, then fopen( ) loads it into memory space and sets up a pointer that tends to the first character in the file. If the file cannot be opened then this fopen( ) function returns NULL.

“w” – Searches for a given file. If the file exists, then it overwrites the contents. Suppose that file doesn’t exist then it creates a new file. There may be a situation when not able to open the file. It simply returns NULL.

“a” – At first search the given file. If the file opens correctly, fopen( ) loads it into memory and sets up a pointer that points to the last character in it. If the file doesn’t exist in this situation, it creates a new file with the same name. Returns NULL, if unable to open the file

How to Read Data from the File ?

During file handling in c programming, if we want to read from a file. The file read operations can be perform using two functions fscanf() or fgets() functions to reading a file in c.

Both the functions do the same operations as that of printf() and gets(), but these functions need file pointer as an additional parameter.

Code snippet for reading a file is as follow-

FILE * fp;

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

fscanf(fp, “%s %s %s %d”, str1, str2, str3, &year);

Writing into a file

During file handling in c programming 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 *fp ;
fp = fopen(“fileName.txt”, “w”);
fprintf(fp, “%s %s %s %d”, “We”, “are”, “in”, 2018);

Close a File

In file handling in c language after performing work on file when we want to close it, we use fclose() function which disconnects a file pointer from the file. 
The objective behind to disconnect the file pointer from the file is to make pointer available to access any other file.
Systems have a limit on the number of files that can be open simultaneously, so it is a good idea to close a file when you have finished using it. 
                                             fclose(output_file);
If files are still open when a program exits, the system will close them for you. However, it is usually better to close the files properly.

EOF The End of File Marker

In file handling in c programming, EOF is a character that indicates the end of a file. getc() and scanf() functions returns it when these functions try to read beyond the limit of the end of a file.
When we perform a read operation on file then to check whether or not you have reached the end of the file , feof() function does this.
if (feof (“filename.txt”) ) printf ( “ABCn” ) ;

Conclusion and Summary

I hope that file handling in c programming tutorial will be useful for computer science students to understand the file handling concepts in c such as file handling functions in c and their use, concepts of file pointer in c, c program to read and write to a file, opening and closing a file.

Leave a Reply

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