Introduction
In this chapter, we will focus on file handling in C. File handling is an essential aspect of programming that allows you to store data persistently, read data from files, and write data to files. Understanding file handling is crucial for developing applications that require data storage and retrieval.
File Handling Basics
In C, file handling is performed using file pointers and functions provided by the standard I/O library (<stdio.h>). The key functions used for file handling include:
fopen– Open a file.fclose– Close a file.fgetc– Read a character from a file.fputc– Write a character to a file.fgets– Read a string from a file.fputs– Write a string to a file.fread– Read data from a file.fwrite– Write data to a file.
Opening and Closing Files
fopen Function
The fopen function is used to open a file. It returns a file pointer that can be used to perform read/write operations on the file.
Syntax:
FILE *fopen(const char *filename, const char *mode);
filename: The name of the file to be opened.mode: The mode in which the file is to be opened (e.g., "r" for read, "w" for write, "a" for append).
fclose Function
The fclose function is used to close an open file. It takes a file pointer as an argument and returns 0 on success or EOF on failure.
Syntax:
int fclose(FILE *stream);
Example: Opening and Closing a File
#include <stdio.h>
int main() {
FILE *file;
// Opening a file in write mode
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Closing the file
fclose(file);
return 0; // Returning 0 to indicate successful execution
}
Output:
No output (but an empty file named example.txt is created)
Writing to a File
fputc Function
The fputc function writes a character to a file.
Syntax:
int fputc(int character, FILE *stream);
Example: Writing a Character to a File
#include <stdio.h>
int main() {
FILE *file;
// Opening a file in write mode
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Writing a character to the file
fputc('A', file);
// Closing the file
fclose(file);
return 0; // Returning 0 to indicate successful execution
}
Output:
No output (but the file example.txt contains the character 'A')
fputs Function
The fputs function writes a string to a file.
Syntax:
int fputs(const char *str, FILE *stream);
Example: Writing a String to a File
#include <stdio.h>
int main() {
FILE *file;
// Opening a file in write mode
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Writing a string to the file
fputs("Hello, World!", file);
// Closing the file
fclose(file);
return 0; // Returning 0 to indicate successful execution
}
Output:
No output (but the file example.txt contains the string "Hello, World!")
Reading from a File
fgetc Function
The fgetc function reads a character from a file.
Syntax:
int fgetc(FILE *stream);
Example: Reading a Character from a File
#include <stdio.h>
int main() {
FILE *file;
char ch;
// Opening a file in read mode
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Reading a character from the file
ch = fgetc(file);
printf("Read character: %c\n", ch);
// Closing the file
fclose(file);
return 0; // Returning 0 to indicate successful execution
}
Output:
Read character: H
fgets Function
The fgets function reads a string from a file.
Syntax:
char *fgets(char *str, int n, FILE *stream);
Example: Reading a String from a File
#include <stdio.h>
int main() {
FILE *file;
char buffer[100];
// Opening a file in read mode
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Reading a string from the file
fgets(buffer, sizeof(buffer), file);
printf("Read string: %s\n", buffer);
// Closing the file
fclose(file);
return 0; // Returning 0 to indicate successful execution
}
Output:
Read string: Hello, World!
Binary File Handling
Binary files store data in a binary format, which is more efficient for storage and retrieval compared to text files. The functions fread and fwrite are used for reading from and writing to binary files.
fread Function
The fread function reads data from a file into a buffer.
Syntax:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
fwrite Function
The fwrite function writes data from a buffer to a file.
Syntax:
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
Example: Writing and Reading Binary Data
#include <stdio.h>
int main() {
FILE *file;
int numbers[5] = {1, 2, 3, 4, 5};
int read_numbers[5];
// Opening a file in write binary mode
file = fopen("binary.dat", "wb");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Writing data to the file
fwrite(numbers, sizeof(int), 5, file);
// Closing the file
fclose(file);
// Opening the file in read binary mode
file = fopen("binary.dat", "rb");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Reading data from the file
fread(read_numbers, sizeof(int), 5, file);
// Printing the read data
printf("Read numbers: ");
for (int i = 0; i < 5; i++) {
printf("%d ", read_numbers[i]);
}
printf("\n");
// Closing the file
fclose(file);
return 0; // Returning 0 to indicate successful execution
}
Output:
Read numbers: 1 2 3 4 5
Conclusion
File handling in C is an essential aspect of programming that allows you to store and retrieve data from files. By understanding and using the standard I/O functions, you can perform various file operations such as opening, closing, reading, and writing files. Mastering file handling is crucial for developing applications that require data storage and retrieval.