File Handling in C
Introduction
File handling is an essential aspect of programming that allows you to read data from files and write data into files in C. This post will provide a comprehensive guide to file handling in C for beginners. We will cover various concepts, functions, and examples to help you understand file handling better.
Table of Contents
- What is File Handling?
- Opening and Closing Files
- Reading from a File
- Reading Characters
- Reading Strings
- Reading Numbers
- Writing to a File
- Writing Characters
- Writing Strings
- Writing Numbers
- File Positioning
- Error Handling
- Conclusion
1. What is File Handling?
File handling is the process of manipulating files stored on a computer's storage system. In C, the "stdio.h" header file provides functions that enable us to work with files. We can perform tasks like reading data from files, writing data into files, and positioning within files using these functions.
2. Opening and Closing Files
Before we can read from or write to a file, we need to open it. We use the fopen() function to open a file and obtain a file pointer, which is a special data type used to interact with the file. The fopen() function requires the file path and the file mode as arguments. Common file modes are "r" for reading, "w" for writing, and "a" for appending.
Example - Opening and Closing a File:
#include <stdio.h>
int main() {
FILE* file_ptr;
file_ptr = fopen("example.txt", "w");
if (file_ptr == NULL) {
printf("File could not be opened.");
return 1;
}
// File operations can be performed here.
fclose(file_ptr);
return 0;
}
3. Reading from a File
We can read data from a file using functions like fgetc() for reading characters, fgets() for reading strings, and fscanf() for reading formatted data.
3.1. Reading Characters
#include <stdio.h>
int main() {
FILE* file_ptr;
file_ptr = fopen("example.txt", "r");
if (file_ptr == NULL) {
printf("File could not be opened.");
return 1;
}
char ch;
while ((ch = fgetc(file_ptr)) != EOF) {
putchar(ch);
}
fclose(file_ptr);
return 0;
}
3.2. Reading Strings
#include <stdio.h>
int main() {
FILE* file_ptr;
file_ptr = fopen("example.txt", "r");
if (file_ptr == NULL) {
printf("File could not be opened.");
return 1;
}
char buffer[100];
while (fgets(buffer, sizeof(buffer), file_ptr) != NULL) {
printf("%s", buffer);
}
fclose(file_ptr);
return 0;
}
3.3. Reading Numbers
#include <stdio.h>
int main() {
FILE* file_ptr;
file_ptr = fopen("example.txt", "r");
if (file_ptr == NULL) {
printf("File could not be opened.");
return 1;
}
int number;
while (fscanf(file_ptr, "%d", &number) != EOF) {
printf("%d ", number);
}
fclose(file_ptr);
return 0;
}
4. Writing to a File
We can write data to a file using functions like fputc() for writing characters, fputs() for writing strings, and fprintf() for writing formatted data.
4.1. Writing Characters
#include <stdio.h>
int main() {
FILE* file_ptr;
file_ptr = fopen("example.txt", "w");
if (file_ptr == NULL) {
printf("File could not be opened.");
return 1;
}
char ch = 'A';
fputc(ch, file_ptr);
fclose(file_ptr);
return 0;
}
4.2. Writing Strings
#include <stdio.h>
int main() {
FILE* file_ptr;
file_ptr = fopen("example.txt", "w");
if (file_ptr == NULL) {
printf("File could not be opened.");
return 1;
}
char str[] = "Hello, File Handling!";
fputs(str, file_ptr);
fclose(file_ptr);
return 0;
}
4.3. Writing Numbers
#include <stdio.h>
int main() {
FILE* file_ptr;
file_ptr = fopen("example.txt", "w");
if (file_ptr == NULL) {
printf("File could not be opened.");
return 1;
}
int number = 42;
fprintf(file_ptr, "%d", number);
fclose(file_ptr);
return 0;
}
5. File Positioning
The fseek() function allows us to set the file pointer at a specific position within the file. This function is useful when we want to read or write data from a particular location.
Example - File Positioning:
#include <stdio.h>
int main() {
FILE* file_ptr;
file_ptr = fopen("example.txt", "r");
if (file_ptr == NULL) {
printf("File could not be opened.");
return 1;
}
fseek(file_ptr, 10, SEEK_SET); // Set the file pointer at 10th byte from the beginning.
char ch;
while ((ch = fgetc(file_ptr)) != EOF) {
putchar(ch);
}
fclose(file_ptr);
return 0;
}
6. Error Handling
When working with files, it's essential to handle errors appropriately. Always check whether the file is successfully opened before performing any file operations.
7. Conclusion
File handling in C is a powerful feature that enables us to read and write data from and into files. By understanding the various file handling functions, you can effectively manage files and store data for future use in your C programs. Remember to handle errors properly and close the file once you are done with the operations.
This concludes our guide to file handling in C for beginners. Happy coding!
0 Comments