01/10/2018, 22:08
[C/C++] Copy file in C
[C/C++] Copy file in C Tháng Mười Một 21, 2013 nguyenvanquan7826 LT C - C++ Leave a response Code dưới đây sẽ copy nội dung của file bạn nhập vào sang 1 file khác cũng do bạn nhập tên. Nếu không thấy file nguồn thì thoát. Lưu ý ...
[C/C++] Copy file in C
Code dưới đây sẽ copy nội dung của file bạn nhập vào sang 1 file khác cũng do bạn nhập tên. Nếu không thấy file nguồn thì thoát. Lưu ý là các file có thể cùng hoặc khác thư mục.
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp1,*fp2;
char ch,f1[1000],f2[1000];
printf("Enter source file name(ex:source.txt): ");
scanf("%s",f1);
fp1=fopen(f1,"r");
if(fp1==NULL)
{
printf("File could not open!!");
return 0;
}
printf("Enter destination file name(ex:destination.txt): ");
scanf("%s",f2);
fp2=fopen(f2,"w");
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
printf("%s is created and copied", f2);
fclose(fp1);
fclose(fp2);
return 0;
}
