In this article, we will write a C program to copy the content of one file into another.
The program takes the name of the source and target files as input from the user, reads the content of the source file, and copies it into the target file. At the end, it prints a success message on the output window.
Sample Input:
Enter the name of the source file: source.txt Enter the name of the target file: target.txt
Sample Output:
Contents of source.txt copied into target.txt successfully
To copy the content of one file into another in C programming language, you mainly need three built-in functions, fopen(), fgetc() and fputc().
The fopen()
function is used to open a file in a specified mode such as read(“r”), write(“w”), etc.
The fgetc()
function is used to read each character of a file one by one. The fputc()
function on the other hand is to write content into a file.
The fopen() needs two parameters first, the name or path of the file which you want to open and second, the mode in which you want to open the file which can be “r”, “w”, etc.
FILE *fopen(const char *filename, const char *mode);
If the file successfully opens, it returns a FILE
pointer which points to the first character of the file.
But, If some error occurs, or the file does not exist, the fopen()
function returns a NULL
pointer.
So, to copy the content of one file into another all you have to do is, open the source file in read(“r”) mode and the target file in write(“w”) mode using the fopen()
function.
Once both files are successfully opened, start reading each character of the source file using the fgetc()
function and write into the target file using the fputc()
function.
See implementation in the following C program:
// C program to copy the content // of one file into another #include <stdio.h> int main(){ char src_filename[100], target_filename[100]; char ch; printf("Enter the name of the source file: "); scanf("%s", &src_filename); printf("Enter the name of the target file: "); scanf("%s", &target_filename); // Try to open both files FILE *fptr_src = fopen(src_filename, "r"); // In Read mode FILE *fptr_target = fopen(target_filename, "w"); // In Write mode if(fptr_src == NULL){ printf("Source File does not exist or cannot be opened."); return 1; } if(fptr_target == NULL){ printf("Target File does not exist or cannot be opened."); return 1; } // Call the fgetc() function on source file ch = fgetc(fptr_src); // Continue calling fgetc() until EOF reached while(ch != EOF){ fputc(ch, fptr_target); // Copy content into target file ch = fgetc(fptr_src); // Read next character from src file } printf("Contents of %s copied into %s successfully", src_filename, target_filename); // Close the file at the end fclose(fptr_src); fclose(fptr_target); return 0; }
Output:
Enter the name of the source file: source.txt Enter the name of the target file: target.txt Contents of source.txt copied into target.txt successfully Enter the name of the source file: random.txt Enter the name of the target file: target.txt Source File does not exist or cannot be opened.
Program Explanation:
- User Input:
- The program starts by prompting the user to enter the name of the source file and the target file.
- The file names are stored in the
src_filename
andtarget_filename
character arrays.
- File Opening:
- The program attempts to open both the source and target files.
- It opens the source file in read mode (“r”) and the target file in write mode (“w”).
- Error checking is performed to ensure that the files exist and can be opened. If any of the files cannot be opened, an error message is displayed, and the program exits with a return code of 1.
- File Copying:
- The program enters a loop to copy the content of the source file into the target file.
- Inside the loop, it uses the
fgetc()
function to read a character from the source file (fptr_src
). - The character read is stored in the variable
ch
.
- End-of-File Check:
- The program checks if the character
ch
is equal toEOF
(End of File). - If
ch
is not equal toEOF
, it means there is more content to read in the source file, and the program proceeds with copying. - If
ch
isEOF
, it indicates that the entire source file has been read, and the loop exits.
- The program checks if the character
- Copying Content:
- Inside the loop, the program uses the
fputc()
function to write the characterch
into the target file (fptr_target
). - This effectively copies the content of the source file into the target file character by character.
- After writing the character, the program reads the next character from the source file.
- Inside the loop, the program uses the
- File Closure:
- After copying is done and the success message is printed, the program closes both the source and target files using
fclose()
to release resources
- After copying is done and the success message is printed, the program closes both the source and target files using
Always remember to close the file at the end using the fclose()
function.
That’s all for this article. Thanks for reading!