What is the purpose of file handling in C?
a) To read from and write to files stored on a disk
b) To store variables in memory
c) To dynamically allocate memory
d) To manage pointers
Answer:
a) To read from and write to files stored on a disk
Explanation:
File handling in C refers to the process of reading from and writing to files stored on a disk. This allows programs to store data persistently, even after the program has terminated. The standard C library provides a set of functions for file handling, including fopen()
for opening files, fclose()
for closing files, fread()
and fwrite()
for reading from and writing to files, and fprintf()
and fscanf()
for formatted input and output. File handling is essential for creating applications that require persistent storage, such as databases and loggers.
Understanding file handling is crucial for managing persistent data in C programming, enabling the creation of applications that interact with the file system.