Here’s a detailed note on File Handling in C:
File handling allows programs to read, write, and manipulate data stored in files. It is essential for data persistence, as files retain data even after the program terminates.
C provides several standard functions for file handling, which are defined in the <stdio.h>
library.
Operation | Function | Description |
---|---|---|
Opening a file | fopen | Opens a file in a specific mode. |
Closing a file | fclose | Closes an open file. |
Reading from a file | fgetc , fgets , fread | Reads data from a file. |
Writing to a file | fputc , fputs , fwrite | Writes data to a file. |
Repositioning | fseek , ftell | Moves the file pointer to a specific position. |
Error handling | feof , ferror | Detects end-of-file or errors. |
When opening a file, you specify a mode to determine its behavior.
Mode | Description |
---|---|
r | Opens a file for reading. |
w | Opens a file for writing (creates or overwrites). |
a | Opens a file for appending. |
r+ | Opens a file for reading and writing. |
w+ | Opens a file for reading and writing (creates or overwrites). |
a+ | Opens a file for reading and appending. |
The FILE
data type is used to manage file operations.
FILE *fp; // Declares a file pointer
FILE *fp = fopen("file.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
}
fclose(fp);
fgetc
(Character-by-Character)FILE *fp = fopen("file.txt", "r");
char ch;
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
fclose(fp);
fgets
(Line-by-Line)FILE *fp = fopen("file.txt", "r");
char buffer[100];
while (fgets(buffer, sizeof(buffer), fp)) {
printf("%s", buffer);
}
fclose(fp);
fread
(Block of Data)struct Data {
int id;
char name[20];
};
FILE *fp = fopen("data.bin", "rb");
struct Data d;
fread(&d, sizeof(d), 1, fp);
printf("ID: %d, Name: %s\n", d.id, d.name);
fclose(fp);
fputc
(Character-by-Character)FILE *fp = fopen("file.txt", "w");
fputc('A', fp);
fclose(fp);
fputs
(String)FILE *fp = fopen("file.txt", "w");
fputs("Hello, World!", fp);
fclose(fp);
fwrite
(Block of Data)struct Data {
int id;
char name[20];
};
FILE *fp = fopen("data.bin", "wb");
struct Data d = {1, "Alice"};
fwrite(&d, sizeof(d), 1, fp);
fclose(fp);
fseek
Moves the file pointer to a specific position.
fseek(fp, offset, SEEK_SET); // From the beginning
fseek(fp, offset, SEEK_CUR); // From the current position
fseek(fp, offset, SEEK_END); // From the end
ftell
Returns the current position of the file pointer.
long position = ftell(fp);
rewind
Resets the file pointer to the beginning.
rewind(fp);
if (feof(fp)) {
printf("End of file reached.\n");
}
if (ferror(fp)) {
printf("Error reading file.\n");
}
#include <stdio.h>
int main() {
FILE *source, *destination;
char ch;
source = fopen("source.txt", "r");
destination = fopen("destination.txt", "w");
if (source == NULL || destination == NULL) {
printf("Error opening file.\n");
return 1;
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
printf("File copied successfully.\n");
fclose(source);
fclose(destination);
return 0;
}