What is File Handling in C Language?

File processing in C is a process that allows you to permanently write and save multiple bytes of data to disk. This allows you to find and reference the relevant data at a later time. File processing in C uses a file type structure pointer to declare the file. For example, an application has been developed and needs to save some important file settings. It must support file processing to permanently store configuration data for reference and later operations.

File Handling Functions in C Language:

The following are the most important file management functions available in “C”.

Attributes to File Handling:

To create a new file with multiple file processing attributes:

Here are some of the main functions that have syntax to perform some common operations:

1. Reading from a file:

Reading a file involves the use of fscanf and fgets. Both functions are similar to the fact that they have the same functionality, except that they use an additional parameter, the file pointer, and read the file easily.


FILE * filePointer;

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

fscanf (filePointer, “%s %s %s %d”, str1, str2, str3, &date);

2. Writing a file:

Writing to a file can be done using the fprintf and fputs functions in the same way as a read operation.


FILE *filePointer;

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

fprintf (filePointer, “%s %s %s %d”, “we”, “live”, “in”,2020);

3. Closing a file:

If all operations are successful and you are always prompted to close the file, you must use the fclose function to close the file.


FILE *filePointer;

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

# Perform some file operations and then close it


For example:

Here Code:

#include <stdio.h>
#include <string.h>
int main ()
	FILE *filePointer;
	char dataToWrite [50] = "ProgrammersAcademy - portal for learning";
	filePointer = fopen ("file_handling_test.c", "w");
if (filePointer == NULL)
	printf ("file_handling_test.c file fails to get open.");
	printf ("The file gets opened.\n");
if (strlen (dataToWrite) > 0)
	fputs (dataToWrite, filePointer);
	fputs ("\n", filePointer);
	printf ("Data gets successfully written in file file_handling_test.c\n");
	printf ("File now gets closed.");
	return 0;


File processing in programming languages, not just C, plays a very important role, especially in industry, because it stores data permanently in memory and can be referenced at any later time. This is a special feature of the file processing function.


(Visited 435 times, 1 visits today)

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Would love your thoughts, please comment.x