File Handling
- Siddharth Sharma
- Dec 19, 2024
- 2 min read
File handling in programming refers to the process of creating, reading, writing, and managing files. It allows data to be stored and retrieved from secondary storage (e.g., hard drives), enabling persistent storage beyond the program's execution.
Structure of File Handling
File handling typically involves the following steps:
Open the File: Specify the file name and mode (e.g., read, write, append).
Perform Operations: Read from or write to the file as needed.
Close the File: Release resources associated with the file.
File Types
Text Files: Store data in a human-readable format, typically as plain text.
Binary Files: Store data in a non-human-readable format, allowing more efficient storage and retrieval.
File Handling Functions (C Language)
In C, file handling is managed using the <stdio.h> library, which provides the following functions:
fopen()
Opens a file in the specified mode (e.g., read, write, append).
Returns a FILE * pointer.
Example:
FILE *fp = fopen("example.txt", "r");fclose()
Closes an opened file.
Example:
fclose(fp);fprintf() and fscanf()
Used for writing to and reading from text files, respectively.
Example:
fprintf(fp, "Hello, World!\n"); fscanf(fp, "%d", &value);fwrite() and fread()
Used for writing to and reading from binary files.
Example:
fwrite(&data, sizeof(data), 1, fp); fread(&data, sizeof(data), 1, fp);fseek() and ftell()
Used for navigating within a file.
Example:
fseek(fp, 0, SEEK_END); long size = ftell(fp);feof() and ferror()
Used for detecting the end of a file and errors, respectively.
Example:
if (feof(fp)) { printf("End of file reached.\n"); } if (ferror(fp)) { printf("Error occurred.\n"); }Buffered vs. Unbuffered Files
Buffered Files: Use an intermediate buffer to improve I/O performance by reducing direct disk access.
Example: Standard file handling functions like fopen() are buffered.
Unbuffered Files: Do not use intermediate buffers, directly interacting with the disk.
Example: Low-level system calls like open().
Error Handling in File Operations
Error handling ensures the program gracefully manages issues such as missing files or write failures. Common techniques include:
Checking the return value of file functions (e.g., fopen() returns NULL on failure).
Using perror() to print error messages.
Example:
FILE *fp = fopen("nonexistent.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}Example: File Handling in C
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char filename[] = "example.txt";
char data[100];
// Open file for writing
fp = fopen(filename, "w");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
fprintf(fp, "Hello, File Handling!\n");
fclose(fp);
// Open file for reading
fp = fopen(filename, "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
while (fgets(data, sizeof(data), fp) != NULL) {
printf("%s", data);
}
fclose(fp);
return 0;
}This example demonstrates basic file operations, including writing to and reading from a text file. Always ensure files are properly closed to release resources.




Comments