C Program for File Handling
A file represents a sequence of bytes on the disk where a group of related data is stored. You can say, a file is a storage area for data. It is a ready-made structure.
To write a C program to create a file or read data from a file, we have to establish a buffer area. That is created using the following syntax:
Here, fp
is a pointer that points to a buffer area and FILE
is written in uppercase. fp
indicates the beginning of the buffer area.
To read the name and roll number of a student, first we have to open the exam.dat
file in read mode "r"
and check if the file exists or not. If it exists, we will retrieve the content from the file using fscanf
and display it using printf
.
Here is the program:
Conclusion
File handling in C is a crucial concept for storing and retrieving data efficiently. By using files, programs can save data permanently, read it when needed, and manage large amounts of information. Understanding how to create, read, and close files ensures better data management and program functionality.