C 程序:将一个文件的内容复制到另一个文件
原文:https://www.studytonight.com/c/programs/files-and-streams/program-copy-file-to-another-file
我们已经知道如何打开文件、读取文件内容和写入文件。所以在这个程序中,我们将从一个文件中读取,同时写入另一个文件,直到我们到达第一个文件的末尾。
#include<stdio.h>
#include<stdio.h>
void main()
{
/*
File_1.txt is the file with content and,
File_2.txt is the file in which content of File_1
will be copied.
*/
FILE *fp1, *fp2;
char ch;
int pos;
if ((fp1 = fopen("File_1.txt", "r")) == NULL)
{
printf("\nFile cannot be opened.");
return;
}
else
{
printf("\nFile opened for copy...\n ");
}
fp2 = fopen("File_2.txt", "w");
fseek(fp1, 0L, SEEK_END); // File pointer at end of file
pos = ftell(fp1);
fseek(fp1, 0L, SEEK_SET); // File pointer set at start
while (pos--)
{
ch = fgetc(fp1); // Copying file character by character
fputc(ch, fp2);
}
fcloseall();
}