[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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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;
}

copy file in C