
File Handling in C
File Operations (open, close, read, write)
File handling in C allows you to store data in a file, read data from a file, and manipulate the data stored in a file. The standard library functions used for file handling are:
fopen()
: Opens a file.fclose()
: Closes an opened file.fprintf()
: Writes formatted output to a file.fscanf()
: Reads formatted input from a file.fputc()
: Writes a character to a file.fgetc()
: Reads a character from a file.
FILE *file;
file = fopen("example.txt", "w"); // Open file for writing
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(file, "Hello, World!\n"); // Write to file
fclose(file); // Close the file
Sequential File Access
Sequential file access means reading or writing data in a sequential manner, from the beginning to the end of the file.
FILE *file;
char ch;
file = fopen("example.txt", "r"); // Open file for reading
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
while ((ch = fgetc(file)) != EOF) {
printf("%c", ch); // Print each character
}
fclose(file); // Close the file
Random File Access
Random file access allows you to move the file pointer to a specific location in the file and read or write data at that location.
FILE *file;
file = fopen("example.txt", "r+"); // Open file for reading and writing
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fseek(file, 6, SEEK_SET); // Move file pointer to 6th byte
fprintf(file, "C Programming"); // Write at the new position
fclose(file); // Close the file
Error Handling in File Operations
Error handling is essential when performing file operations to ensure your program can handle situations where file operations fail.
FILE *file;
file = fopen("nonexistent.txt", "r"); // Try to open a nonexistent file
if (file == NULL) {
perror("Error opening file"); // Print error message
return 1;
}
fclose(file); // Close the file (if opened)
Example Programs Demonstrating File Handling
Example 1: Writing to a File
#include <stdio.h>
int main() {
FILE *file = fopen("output.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(file, "This is a test.\n");
fclose(file);
return 0;
}
Example 2: Reading from a File
#include <stdio.h>
int main() {
FILE *file = fopen("output.txt", "r");
char ch;
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
return 0;
}
Example 3: Appending to a File
#include <stdio.h>
int main() {
FILE *file = fopen("output.txt", "a");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(file, "Appended text.\n");
fclose(file);
return 0;
}
Summary
In this post, we covered the basics of file handling in C, including how to open, close, read, and write files. We also discussed sequential and random file access, error handling in file operations, and provided example programs to demonstrate these concepts. Understanding file handling is essential for managing data storage and retrieval in C programming.
For further reading, consider these resources:
0 Comments: