File Handling in C
File handling in C involves using a set of functions provided by the C Standard Library to perform operations on files, such as reading from, writing to, and managing files. Here’s a detailed overview:
File Handling in C Basics
In C, files are managed using file pointers. A file pointer is a variable of type FILE* that holds the address of a file structure used by the system to keep track of the file.
Header File in C
To perform file operations, you need to include the stdio.h header file:
#include <stdio.h>
Opening and Closing Files
To open a file, use the fopen() function. To close a file, use the fclose() function.
Opening a File:
FILE *file = fopen("filename.txt", "mode");
Modes for Opening Files
"r": Read-only mode"w": Write-only mode (creates a new file or truncates an existing file)"a": Append mode (adds data to the end of the file)"r+": Read and write mode"w+": Read and write mode (creates a new file or truncates an existing file)"a+": Read and write mode (appends data to the end of the file)
Example:
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
}
Closing a File:
fclose(file);
Reading from and Writing to Files
Reading:
fgetc(FILE *stream): Reads the next character from the file.fgets(char *str, int n, FILE *stream): Reads a line from the file intostr.fread(void *ptr, size_t size, size_t count, FILE *stream): Readscountelements ofsizebytes from the file.
Example:
char c;
while ((c = fgetc(file)) != EOF) {
putchar(c);
}
Writing:
fputc(int char, FILE *stream): Writes a character to the file.fputs(const char *str, FILE *stream): Writes a string to the filefwrite(const void *ptr, size_t size, size_t count, FILE *stream): Writescountelements ofsizebytes to the file.
Example:
fprintf(file, "Hello, World!\n");
File Positioning
fseek(FILE *stream, long int offset, int whence): Moves the file pointer to a specified location.offset: Number of bytes to move.whence: Position from where to move (SEEK_SET,SEEK_CUR,SEEK_END)
Example:
fseek(file, 0, SEEK_SET); // Move to the beginning of the file
ftell(FILE *stream): Returns the current position of the file pointer.rewind(FILE *stream): Sets the file position back to the beginning of the file.
File Status Functions
feof(FILE *stream): Checks for the end-of-file condition.ferror(FILE *stream): Checks for errors during file operations.clearerr(FILE *stream): Clears the end-of-file and error indicators.
Example Program
Here’s a simple example demonstrating file handling in C:
#include <stdio.h>
int main() {
// Open a file for writing
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file for writing!\n");
return 1;
}
// Write to the file
fprintf(file, "Hello, File Handling in C!\n");
fclose(file); // Close the file
// Open the file for reading
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file for reading!\n");
return 1;
}
// Read and print the file contents
char buffer[100];
while (fgets(buffer, sizeof(buffer), file)) {
printf("%s", buffer);
}
fclose(file); // Close the file
return 0;
}
Conclusion
File handling in C is essential for many programming tasks, from simple file reading and writing to complex file manipulations. Understanding these basic functions and concepts will help you work with files effectively in your C programs.