File Handling in C Language:
File Handling in C
File handling in C allows you to create, open, read, write, and close files. It provides a way to store data permanently on disk and retrieve it when needed. C provides a set of standard library functions for file handling operations.
File Handling Functions
fopen()
: Opens a file.
fclose()
: Closes a file.
fprintf()
: Writes formatted output to a file.
fscanf()
: Reads formatted input from a file.
fputc()
: Writes a character to a file.
fgetc()
: Reads a character from a file.
fputs()
: Writes a string to a file.
fgets()
: Reads a string from a file.
fseek()
: Moves the file pointer to a specified position.
ftell()
: Returns the current position of the file pointer.
rewind()
: Moves the file pointer to the beginning of the file.
Opening and Closing a File
Files can be opened using the fopen()
function. The mode in which the file is opened determines the operations that can be performed on the file.
#include
int main() {
FILE *file;
// Open a file in write mode
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Close the file
fclose(file);
return 0;
}
File Opening Modes
Mode |
Description |
r |
Open for reading. The file must exist. |
w |
Open for writing. Creates a new file if it doesn't exist or truncates the file if it exists. |
a |
Open for appending. Creates a new file if it doesn't exist. |
r+ |
Open for both reading and writing. The file must exist. |
w+ |
Open for both reading and writing. Creates a new file if it doesn't exist or truncates the file if it exists. |
a+ |
Open for both reading and appending. Creates a new file if it doesn't exist. |
Reading and Writing to a File
Writing to a File
#include
int main() {
FILE *file;
// Open a file in write mode
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Write to the file
fprintf(file, "Hello, World!\n");
// Close the file
fclose(file);
return 0;
}
Reading from a File
#include
int main() {
FILE *file;
char buffer[100];
// Open a file in read mode
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Read from the file
fgets(buffer, 100, file);
printf("Read from file: %s", buffer);
// Close the file
fclose(file);
return 0;
}
File Positioning Functions
Moving the File Pointer
#include
int main() {
FILE *file;
long position;
// Open a file in read mode
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Move the file pointer to the end
fseek(file, 0, SEEK_END);
// Get the current position of the file pointer
position = ftell(file);
printf("File size: %ld bytes\n", position);
// Move the file pointer to the beginning
rewind(file);
// Close the file
fclose(file);
return 0;
}
Example: Copying Contents from One File to Another
#include
int main() {
FILE *sourceFile, *destinationFile;
char ch;
// Open the source file in read mode
sourceFile = fopen("source.txt", "r");
if (sourceFile == NULL) {
printf("Error opening source file!\n");
return 1;
}
// Open the destination file in write mode
destinationFile = fopen("destination.txt", "w");
if (destinationFile == NULL) {
printf("Error opening destination file!\n");
fclose(sourceFile);
return 1;
}
// Copy contents from source file to destination file
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destinationFile);
}
printf("File copied successfully!\n");
// Close the files
fclose(sourceFile);
fclose(destinationFile);
return 0;
}