File I/O in C Language
Introduction
File Input/Output (I/O) is a fundamental concept in the C programming language that allows you to read from and write to external files. This is essential for data storage, processing, and retrieval in various applications.
Opening a File
To open a file in C, you use the fopen() function, which is defined in the stdio.h header file. The fopen() function requires two parameters: the name of the file and the mode in which the file should be opened.
Example:
FILE *filePointer;
filePointer = fopen("example.txt", "r");
In this example, filePointer is a pointer to the file, "example.txt" is the name of the file, and "r" stands for the read mode.
File Modes
Different modes available for opening a file include:
"r"- Open for reading. The file must exist."w"- Open for writing. If the file exists, its contents are overwritten. If the file does not exist, it is created."a"- Open for appending. Data is added to the end of the file. If the file does not exist, it is created."r+"- Open for both reading and writing. The file must exist."w+"- Open for both reading and writing. If the file exists, its contents are overwritten. If the file does not exist, it is created."a+"- Open for both reading and appending. Data is added to the end of the file. If the file does not exist, it is created.
Closing a File
After you are done with a file, it is a good practice to close it using the fclose() function. This frees up resources used by the file.
Example:
fclose(filePointer);
Writing to a File
To write data to a file, you can use functions like fprintf(), fputs(), or fwrite().
Example using fprintf():
FILE *filePointer = fopen("example.txt", "w");
if (filePointer != NULL) {
fprintf(filePointer, "Hello, World!\n");
fclose(filePointer);
}
Reading from a File
To read data from a file, you can use functions like fscanf(), fgets(), or fread().
Example using fgets():
FILE *filePointer = fopen("example.txt", "r");
char buffer[100];
if (filePointer != NULL) {
while (fgets(buffer, 100, filePointer) != NULL) {
printf("%s", buffer);
}
fclose(filePointer);
}
Error Handling
It is important to handle errors while performing file I/O operations. You can use the perror() function to print a descriptive error message if a file operation fails.
Example:
FILE *filePointer = fopen("nonexistent.txt", "r");
if (filePointer == NULL) {
perror("Error opening file");
}
Example Program
Here is a complete example program that demonstrates how to open a file, write to it, read from it, and handle errors:
#include <stdio.h>
int main() {
FILE *filePointer;
// Open file for writing
filePointer = fopen("example.txt", "w");
if (filePointer == NULL) {
perror("Error opening file for writing");
return 1;
}
fprintf(filePointer, "Hello, World!\n");
fclose(filePointer);
// Open file for reading
filePointer = fopen("example.txt", "r");
if (filePointer == NULL) {
perror("Error opening file for reading");
return 1;
}
char buffer[100];
while (fgets(buffer, 100, filePointer) != NULL) {
printf("%s", buffer);
}
fclose(filePointer);
return 0;
}
