File I/O is a fundamental skill in systems programming. This C++ program demonstrates how to copy the contents of one text file to another using standard C file functions: fopen(), fgetc(), fputc(), feof(), and fclose(). The program opens the source file in read mode and the destination file in write mode, then reads characters one at a time from the source and writes each character to the destination until the end of the source file is reached.
This approach is a classic example of character-level file copying — simple, portable, and easy to trace. It also shows basic error handling: if the source file cannot be opened (e.g., path does not exist), the program reports an error instead of proceeding.
C++ Program: Copy Text from One File to Another
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <io.h>
void main()
{
clrscr();
// Open source file for reading and destination file for writing
FILE *sourceFile, *destinationFile;
sourceFile = fopen("C:\\am\\ankur.txt", "r");
destinationFile = fopen("D:\\am\\mhatre.txt", "w");
int endOfFile = -1; // fgetc() returns -1 (EOF) when the end of file is reached
// Read the first character to check if the source file opened successfully
char currentChar = fgetc(sourceFile);
if (currentChar == endOfFile)
{
cout << "Error in opening file";
}
else
{
// Write the first character that was already read
fputc(currentChar, destinationFile);
// Read and copy the rest of the file character by character
while (!feof(sourceFile))
{
char currentChar = fgetc(sourceFile); // Read next character
if (currentChar != endOfFile) // Skip the EOF sentinel
{
fputc(currentChar, destinationFile); // Write to destination
}
}
cout << "File copied successfully";
}
fclose(sourceFile);
fclose(destinationFile);
getch();
}
How the Code Works
Step 1 — Open Files: fopen() is called with "r" (read) for the source and "w" (write) for the destination. Note the double backslashes in the file paths (C:amankur.txt) — in C/C++ string literals, a single must be written as to represent one literal backslash character in the path.
Step 2 — First Character Check: The program reads the very first character before entering the loop. If fgetc() immediately returns -1 (EOF), the source file could not be opened or is empty, and an error message is displayed.
Step 3 — Character-by-Character Copy: Inside the while (!feof(sourceFile)) loop, each character is read with fgetc() and written to the destination file with fputc(). The EOF sentinel check ensures the literal EOF value is never written as a byte to the output file.
Step 4 — Close Files: Both file handles are closed with fclose() to flush buffers and release OS resources. Skipping this step on the destination file can result in the last few bytes not being written to disk.
Sample Output
File copied successfully
Output Explanation
When the source file C:amankur.txt exists and is readable, the program reads every character and writes it to D:ammhatre.txt, then prints File copied successfully. If the source path is wrong or the file does not exist, fgetc() returns -1 immediately and the program prints Error in opening file instead. After execution, the destination file is an exact byte-for-byte copy of the source.
See Also
- Implementation of Hamming Code in C++ — error correction in data communication
- Implementation of CRC Algorithm in C++ — error detection using polynomial division
- Implementation of Dijkstra Algorithm in C++ — shortest path routing algorithm
- Implementation of Distance Vector Routing (DVR) Algorithm in C++ — distributed routing protocol
- Implementing Socket Programming in Java — TCP client-server communication
Conclusion
Character-level file copying is the simplest and most transparent form of file I/O in C/C++. While modern code would typically use std::filesystem::copy or buffered stream classes, this approach clearly exposes how the OS file pointer advances one byte at a time, making it an excellent learning exercise. The same pattern — open, loop until EOF, close — is the foundation for text processing, log parsing, and any other task that operates on file contents one character at a time.