File Handling in C:
File handling allows a C program to read from and write to files. This is an essential concept for
working with data persistence in C programming. Files can be of two main types:
1. Text Files – Contains readable data (ASCII characters).
2. Binary Files – Contains data in binary format (non-readable).
Basic File Operations:
In C, files can be manipulated using a set of functions provided in the stdio.h library. The most
commonly used file functions are:
1. fopen() – Opens a file.
2. fclose() – Closes a file.
3. fprintf() / fputs() – Write to a file.
4. fscanf() / fgets() – Read from a file.
5. fseek() – Move the file pointer.
6. ftell() – Get the current position of the file pointer.
7. feof() – Check for end of file.
File Opening Modes:
The fopen() function is used to open a file. It takes two arguments: the name of the file and the
mode in which the file should be opened. The modes are:
• "r" – Open a file for reading. If the file doesn't exist, the program will fail.
• "w" – Open a file for writing. If the file exists, it is overwritten.
• "a" – Open a file for appending. New data is written at the end of the file.
• "r+" – Open a file for both reading and writing.
• "w+" – Open a file for both reading and writing. It creates a new file or overwrites the
existing file.
• "a+" – Open a file for both reading and appending.
Example Programs for File Handling:
1. Program to Write Data to a File
This program demonstrates how to open a file in "write" mode and write data into it.
#include <stdio.h>
int main() {
FILE *file;
char data[] = "This is a test file containing some text data.";
// Open file for writing
file = fopen("testfile.txt", "w");
if (file == NULL) {
printf("Unable to open the file.\n");
return 1;
}
// Write data to the file
fprintf(file, "%s", data);
// Close the file
fclose(file);
printf("Data has been written to testfile.txt\n");
return 0;
}
Explanation:
• fopen("testfile.txt", "w"): Opens the file testfile.txt in "write" mode. If the file does not exist,
it is created. If the file already exists, its content is overwritten.
• fprintf(): Writes the string data to the file.
• fclose(file): Closes the file to save the data.
2. Program to Read Data from a File
This program demonstrates how to open a file in "read" mode and read data from it.
#include <stdio.h>
int main() {
FILE *file;
char ch;
// Open file for reading
file = fopen("testfile.txt", "r");
if (file == NULL) {
printf("Unable to open the file.\n");
return 1;
}
// Read and print content of the file
while ((ch = fgetc(file)) != EOF) {
printf("%c", ch);
}
// Close the file
fclose(file);
return 0;
}
Explanation:
• fopen("testfile.txt", "r"): Opens the file testfile.txt for reading. If the file does not exist, the
program terminates.
• fgetc(file): Reads a character from the file. It continues reading characters until it encounters
EOF (End Of File).
• fclose(file): Closes the file after reading.
3. Program to Append Data to a File
This program demonstrates how to append data to an existing file.
#include <stdio.h>
int main() {
FILE *file;
char data[] = "\nAppending some new data to the file.";
// Open file for appending
file = fopen("testfile.txt", "a");
if (file == NULL) {
printf("Unable to open the file.\n");
return 1;
}
// Append data to the file
fprintf(file, "%s", data);
// Close the file
fclose(file);
printf("Data has been appended to testfile.txt\n");
return 0;
}
Explanation:
• fopen("testfile.txt", "a"): Opens the file testfile.txt in "append" mode. If the file does not
exist, it is created.
• fprintf(): Appends the data to the file at the end.
• fclose(file): Closes the file after writing.
4. Program to Read Data Using fscanf
This program reads data from a file using the fscanf() function, which works like scanf() but reads
from a file.
#include <stdio.h>
int main() {
FILE *file;
char name[50];
int age;
// Open file for reading
file = fopen("testfile.txt", "r");
if (file == NULL) {
printf("Unable to open the file.\n");
return 1;
}
// Read data from the file
fscanf(file, "%s %d", name, &age);
// Print the data
printf("Name: %s\n", name);
printf("Age: %d\n", age);
// Close the file
fclose(file);
return 0;
}
Explanation:
• fscanf(file, "%s %d", name, &age): Reads a string (%s) and an integer (%d) from the file.
• fclose(file): Closes the file after reading.
5. Program to Copy One File to Another
This program copies the contents of one file to another.
#include <stdio.h>
int main() {
FILE *source, *destination;
char ch;
// Open source file for reading
source = fopen("source.txt", "r");
if (source == NULL) {
printf("Unable to open source file.\n");
return 1;
}
// Open destination file for writing
destination = fopen("destination.txt", "w");
if (destination == NULL) {
printf("Unable to open destination file.\n");
fclose(source);
return 1;
}
// Copy contents from source to destination
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
printf("File has been copied successfully.\n");
// Close both files
fclose(source);
fclose(destination);
return 0;
}
• fopen("source.txt", "r"): Opens the source file for reading.
• fopen("destination.txt", "w"): Opens the destination file for writing.
• fgetc(source): Reads one character at a time from the source file.
• fputc(ch, destination): Writes the character to the destination file.
• fclose(source) and fclose(destination): Close both files after copying.
1. Program to Write Data to a File
This simple program demonstrates how to write data to a file using fprintf().
#include <stdio.h>
int main() {
FILE *file;
file = fopen("testfile.txt", "w"); // Open file for writing
if (file == NULL) {
printf("Unable to open file\n");
return 1;
}
fprintf(file, "Hello, this is a test file.\n");
fprintf(file, "This is written using C file handling.\n");
fclose(file); // Close the file
printf("Data written to file successfully\n");
return 0;
}
Explanation:
• fopen("testfile.txt", "w"): Opens the file in write mode. If the file doesn't exist, it is created.
If it exists, it is overwritten.
• fprintf(file, "text"): Writes the given string to the file.
• fclose(file): Closes the file after writing.
2. Program to Read Data from a File
This program demonstrates how to read data from a file using fgetc().
#include <stdio.h>
int main() {
FILE *file;
char ch;
file = fopen("testfile.txt", "r"); // Open file for reading
if (file == NULL) {
printf("Unable to open file\n");
return 1;
}
// Read and print characters from the file until EOF
while ((ch = fgetc(file)) != EOF) {
printf("%c", ch); // Print each character
}
fclose(file); // Close the file
return 0;
}
Explanation:
• fopen("testfile.txt", "r"): Opens the file in read mode.
• fgetc(file): Reads one character at a time from the file. The loop continues until EOF (End Of
File) is encountered.
• fclose(file): Closes the file after reading.
3. Program to Append Data to a File
This program demonstrates how to append data to an existing file using fprintf().
#include <stdio.h>
int main() {
FILE *file;
// Open file in append mode
file = fopen("testfile.txt", "a");
if (file == NULL) {
printf("Unable to open file\n");
return 1;
}
// Append new data to the file
fprintf(file, "\nThis line is added to the file using append mode.");
fclose(file); // Close the file
printf("Data has been appended to the file\n");
return 0;
}
Explanation:
• fopen("testfile.txt", "a"): Opens the file in append mode. If the file doesn't exist, it is
created.
• fprintf(file, "text"): Appends the given string to the end of the file.
4. Program to Copy One File to Another
This program demonstrates how to copy the contents of one file to another.
#include <stdio.h>
int main() {
FILE *source, *destination;
char ch;
// Open the source file for reading
source = fopen("source.txt", "r");
if (source == NULL) {
printf("Unable to open source file\n");
return 1;
}
// Open the destination file for writing
destination = fopen("destination.txt", "w");
if (destination == NULL) {
printf("Unable to open destination file\n");
fclose(source);
return 1;
}
// Copy content from source to destination
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
printf("File has been copied successfully\n");
fclose(source);
fclose(destination);
return 0;
}
No comments:
Post a Comment