Chapter 11: File Handling
File handling in C allows you to read from and write
to files. It enables you to store data persistently and work with external
files. In this chapter, we will explore how to open, read from, write to, and
close files in C:
11.1
File Pointers:
To work with files in C, you need to declare a file
pointer variable of type `FILE*`. This pointer will be used to access the file.
Example:
#include
<stdio.h>
int
main() {
FILE* filePtr;
// File operations go here
return 0;
}
The program declares a file pointer variable
`filePtr`. We will use this pointer to perform file operations.
11.2
Opening Files:
Before reading from or writing to a file, you need
to open it using the `fopen()` function. It requires the file name and mode as
arguments.
FILE*
fopen(const char* filename, const char* mode);
Example:
#include
<stdio.h>
int
main() {
FILE* filePtr;
// Open a file for writing
filePtr = fopen("file.txt",
"w");
if (filePtr == NULL) {
printf("Unable to open the
file.\n");
return 1;
}
// File operations go here
// Close the file
fclose(filePtr);
return 0;
}
The program opens a file named "file.txt"
in write mode ("w") using `fopen()`. It checks if the file opening
was successful by checking if `filePtr` is NULL. If it is NULL, it means the
file couldn't be opened. Finally, the program closes the file using `fclose()`.
11.3
Writing to Files:
To write data to a file, you can use the `fprintf()`
function, which works similar to `printf()` but writes to a file instead of the
console.
int
fprintf(FILE* stream, const char* format, ...);
Example:
#include
<stdio.h>
int
main() {
FILE* filePtr;
filePtr = fopen("file.txt",
"w");
if (filePtr == NULL) {
printf("Unable to open the
file.\n");
return 1;
}
fprintf(filePtr, "Hello,
World!\n");
fprintf(filePtr, "This is a
file.");
fclose(filePtr);
return 0;
}
The program writes two lines of text to the file
using `fprintf()`. Each `fprintf()` call takes the file pointer and the text to
be written as arguments. Finally, the program closes the file.
11.4
Reading from Files:
To read data from a file, you can use the `fscanf()`
function, which works similar to `scanf()` but reads from a file instead of the
console.
int
fscanf(FILE* stream, const char* format, ...);
Example:
#include
<stdio.h>
int
main() {
FILE* filePtr;
char buffer[100];
filePtr = fopen("file.txt",
"r");
if (filePtr == NULL) {
printf("Unable to open the
file.\n");
return 1;
}
fscanf(filePtr, "%[^\n]",
buffer);
printf("Read from file: %s\n",
buffer);
fclose(filePtr);
return 0;
}
The program reads a line of text from the file using
`fscanf()` and stores it in the `buffer` variable. It then prints the read data
using `printf()`. Finally, the program closes the file.
11.5
Error Handling:
When working with files, it's essential to handle
errors that may occur during file operations. You can use the `feof()` function
to check if the end of the file has been reached.
Example:
#include
<stdio.h>
int
main() {
FILE* filePtr;
char ch;
filePtr = fopen("file.txt",
"r");
if (filePtr == NULL) {
printf("Unable to open the
file.\n");
return 1;
}
while ((ch = fgetc(filePtr)) != EOF) {
printf("%c", ch);
}
fclose(filePtr);
return 0;
}
The program reads and prints each character in the
file until it reaches the end of the file (EOF). The `fgetc()` function is used
to read a single character at a time. The program also checks if the file
opening was successful and handles the error accordingly.
11.6
File Positioning:
You can move the file position indicator using the
`fseek()` function. It allows you to set the position from where the next read
or write operation will occur.
int
fseek(FILE* stream, long int offset, int origin);
Example:
#include
<stdio.h>
int
main() {
FILE* filePtr;
char ch;
filePtr = fopen("file.txt",
"r");
if (filePtr == NULL) {
printf("Unable to open the
file.\n");
return 1;
}
fseek(filePtr, 6, SEEK_SET);
while ((ch = fgetc(filePtr)) != EOF) {
printf("%c", ch);
}
fclose(filePtr);
return 0;
}
The program moves the file position indicator to the
6th position using `fseek()` and `SEEK_SET` as the origin. It then reads and
prints the remaining contents of the file. This demonstrates how you can read
from a specific position in a file.
11.7
Closing Files:
It's essential to close the file after you have
finished working with it to release system resources.
int
fclose(FILE* stream);
Example:
#include
<stdio.h>
int
main() {
FILE* filePtr;
filePtr = fopen("file.txt",
"r");
if (filePtr == NULL) {
printf("Unable to open the
file.\n");
return 1;
}
// File operations go here
fclose(filePtr);
return 0;
}
The program opens a file for reading and performs
file operations. Finally, it closes the file using `fclose()`.
0 Comments