📌  相关文章
📜  用C程序将一个文件的内容复制到另一个文件

📅  最后修改于: 2021-05-26 01:51:25             🧑  作者: Mango

#include 
#include  // For exit()
  
int main()
{
    FILE *fptr1, *fptr2;
    char filename[100], c;
  
    printf("Enter the filename to open for reading \n");
    scanf("%s", filename);
  
    // Open one file for reading
    fptr1 = fopen(filename, "r");
    if (fptr1 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }
  
    printf("Enter the filename to open for writing \n");
    scanf("%s", filename);
  
    // Open another file for writing
    fptr2 = fopen(filename, "w");
    if (fptr2 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }
  
    // Read contents from file
    c = fgetc(fptr1);
    while (c != EOF)
    {
        fputc(c, fptr2);
        c = fgetc(fptr1);
    }
  
    printf("\nContents copied to %s", filename);
  
    fclose(fptr1);
    fclose(fptr2);
    return 0;
}

输出:

Enter the filename to open for reading
a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt 
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。