1.
C Program to Copy One File to Another:
#include <stdio.h>
int main() {
FILE *source, *destination;
char ch;
source = fopen("source.txt", "r"); // Open source file in read
mode
if (source == NULL) {
printf("Error opening source file.\n");
return 1;
}
destination = fopen("destination.txt", "w"); // Open destination
file in write mode
if (destination == NULL) {
printf("Error opening destination file.\n");
fclose(source);
return 1;
}
// Copy character by character
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
fclose(source);
fclose(destination);
printf("File copied successfully.\n");
return 0;
}
2. C Program to Merge Two Files using Command Line Arguments:
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage: %s <file1> <file2> <output_file>\n", argv[0]);
return 1;
}
FILE *file1, *file2, *output;
char ch;
file1 = fopen(argv[1], "r");
if (file1 == NULL) {
printf("Error opening file1.\n");
return 1;
}
file2 = fopen(argv[2], "r");
if (file2 == NULL) {
printf("Error opening file2.\n");
fclose(file1);
return 1;
}
output = fopen(argv[3], "w");
if (output == NULL) {
printf("Error opening output file.\n");
fclose(file1);
fclose(file2);
return 1;
}
// Copy contents of file1
while ((ch = fgetc(file1)) != EOF) {
fputc(ch, output);
}
// Copy contents of file2
while ((ch = fgetc(file2)) != EOF) {
fputc(ch, output);
}
fclose(file1);
fclose(file2);
fclose(output);
printf("Files merged successfully.\n");
return 0;
}
3. File Input/Output Operations:
● a) fprintf(), fscanf():
○ fprintf() writes formatted output to a file.
○ fscanf() reads formatted input from a file.
fprintf(file_pointer, "Hello, world!\n");
fscanf(file_pointer, "%d %f", &integer, &float_value);
● b) fgetc(), fputc():
○ fgetc() reads a single character from a file.
○ fputc() writes a single character to a file.
● c) getw(), putw():
○ getw() reads a single integer from a file.
○ putw() writes a single integer to a file.
● d) fread(), fwrite():
○ fread() reads a block of data from a file.
○ fwrite() writes a block of data to a file.
fread(&data, sizeof(data), 1, file_pointer);
fwrite(&data, sizeof(data), 1, file_pointer);
4. File Status Functions (feof, ferror)
● feof(file_pointer): Returns non-zero if the end-of-file indicator for the specified stream is
set.
● ferror(file_pointer): Returns non-zero if an error occurred on the specified stream.
if (feof(file_pointer)) {
printf("End of file reached.\n");
}
if (ferror(file_pointer)) {
printf("An error occurred during file operations.\n");
}
5. File Positioning Functions (fseek)
● fseek(file_pointer, offset, origin): Moves the file position indicator to a new position within
the file.
○ origin: Can be SEEK_SET (beginning of file), SEEK_CUR (current position), or
SEEK_END (end of file).
fseek(file_pointer, 0, SEEK_SET); // Move to the beginning of the file
fseek(file_pointer, 10, SEEK_CUR); // Move 10 bytes forward from the
current position
fseek(file_pointer, -20, SEEK_END); // Move 20 bytes backward from the
end of the file
6. Error Handling Functions (errno, perror, strerror)
● errno: A global variable that holds the value of the last system error that occurred.
● perror(message): Prints the system error message associated with the current value of
errno, preceded by the given message.
● strerror(errno): Returns a string containing the system error message associated with
the current value of errno.
if (file_pointer == NULL) {
perror("Error opening file");
exit(1);
}
7. Difference Between Text Files and Binary Files
Feature Text File Binary File
Data Representation Stores data as sequences of Stores data in raw binary format
characters (e.g., ASCII)
Read/Write Typically read and written Read and written in blocks of
character by character bytes
Portability May be platform-dependent dueGenerally more portable across
to character encoding different systems
8. File Operations with Examples
● Reading from a file:
○ fgetc(), fscanf(), fread()
● Writing to a file:
○ fputc(), fprintf(), fwrite()
● Appending to a file:
○ Open the file in append mode ("a" or "a+") using fopen().
9. File Mode Operations
● "r": Open for reading.
● "w": Open for writing. Creates a new file or truncates an existing file.
● "a": Open for writing. Appends data to the end of the file.
● "r+": Open for both reading and writing.
● "w+": Open for both reading and writing. Creates a new file or truncates an existing file.
● "a+": Open for both reading and writing. Appends data to the end of the file.
● "rb": Open for reading in binary mode.
● "wb": Open for writing in binary mode.
● "ab": Open for appending in binary mode.
10. File Handling Functions (with Example)
● fopen(): Opens a file and returns a file pointer.
● fclose(): Closes an open file.
● fseek(): Moves the file position indicator.
● ftell(): Gets the current position within a file.
● rewind(): Moves the file position indicator to the beginning of the file.
FILE *file_pointer = fopen("my_file.txt", "r");
if (file_pointer == NULL) {
// Handle error
}
// ... perform file operations ...
fclose(file_pointer);
11. Reading and Writing Binary and Text Files
● Binary Files:
○ Use fread() and fwrite() for reading and writing blocks of data.
○ Ensure proper data alignment and byte order when reading and writing binary data.
● Text Files:
○ Use fgetc(), fputc(), fscanf(), and fprintf() for character-based or formatted I/O.
12. Multi-File Operations
● Involve working with multiple files simultaneously.
● Examples:
○ Merging files (as shown in example 2)
○ Comparing files
○ Splitting a large file into smaller files