This section lists the programs covered under File Handling and Command Line Arguments in C. Click on a program title to jump directly to its detailed explanation.
Write a C program to merge the contents of two files into a third file.
#include <stdio.h>
int main() {
FILE *f1, *f2, *f3;
char ch;
f1 = fopen("file1.txt", "r");
f2 = fopen("file2.txt", "r");
f3 = fopen("file3.txt", "w");
if (f1 == NULL || f2 == NULL || f3 == NULL) {
printf("File error\n");
return 0;
}
while ((ch = fgetc(f1)) != EOF)
fputc(ch, f3);
while ((ch = fgetc(f2)) != EOF)
fputc(ch, f3);
fclose(f1);
fclose(f2);
fclose(f3);
printf("Files merged successfully\n");
return 0;
}
Files merged successfully
The program uses fopen() to associate filenames with file pointers and checks for NULL to handle opening errors safely. Characters are copied from the first and then the second file using fgetc() to read and fputc() to write, preserving the original contents.
After copying, all files are closed using fclose() to release system resources. The third file file3.txt ends up containing the text of file1.txt immediately followed by the text of file2.txt.
Write a C program to reverse the contents of a file.
fseek().ftell() to get the current position (file size in bytes).fgetc() and print it.
#include <stdio.h>
int main() {
FILE *fp;
char ch;
long pos;
fp = fopen("file.txt", "r");
if (fp == NULL) {
printf("File not found\n");
return 0;
}
fseek(fp, 0, SEEK_END);
pos = ftell(fp);
while (pos--) {
fseek(fp, pos, SEEK_SET);
ch = fgetc(fp);
printf("%c", ch);
}
fclose(fp);
return 0;
}
(If file.txt contains: HELLO)
OLLEH
The function fseek() moves the file pointer to any byte position in the file, and ftell() reports the current position, which is used here to determine the file size. Starting from the last byte and moving backwards allows the program to read characters in reverse order.
This technique demonstrates random (non-sequential) access to file contents, which is useful when only specific parts of a file need to be read or modified without scanning it from the beginning.
Write a C program to demonstrate random access functions in files.
fseek().fgetc().
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("file.txt", "r");
if (fp == NULL) {
printf("File not found\n");
return 0;
}
fseek(fp, 5, SEEK_SET);
ch = fgetc(fp);
printf("Character at position 6: %c\n", ch);
fclose(fp);
return 0;
}
(If file.txt contains: ABCDEF)
Character at position 6: F
The call fseek(fp, 5, SEEK_SET) moves the file pointer to the 6th character in the file (because positions start from 0). The next fgetc() then reads exactly that character without reading earlier ones.
This example highlights how random access lets programs “jump” directly to any location in a file, which is especially useful when working with large files or fixed-size records.
Write a C program to count the number of times a character occurs in a text file. The file name and character are supplied as command line arguments.
argc and argv to read the command line arguments.argv[1].fgetc().argv[2][0], increment a counter.
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *fp;
char ch;
int count = 0;
if (argc != 3) {
printf("Usage: %s filename character\n", argv[0]);
return 0;
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("File not found\n");
return 0;
}
while ((ch = fgetc(fp)) != EOF) {
if (ch == argv[2][0])
count++;
}
fclose(fp);
printf("Character '%c' occurs %d times\n", argv[2][0], count);
return 0;
}
Command:
./program sample.txt a
Output:
Character 'a' occurs 7 times
The parameters argc and argv allow the program to accept inputs from the command line instead of scanf(). Here, argv[1] holds the filename and argv[2][0] holds the character to be counted.
The file is scanned from beginning to end, and every time the current character equals the target character, the counter is increased by one. After the loop finishes, the program reports the total count, demonstrating both file handling and basic use of command line arguments.