【问题标题】:How to copy content of one file to another in C如何在C中将一个文件的内容复制到另一个文件
【发布时间】:2015-10-28 20:01:18
【问题描述】:
#include<stdio.h>
#include<process.h>

void main() {
FILE *fp1, *fp2;
char a;

fp1 = fopen("input.txt", "r");
if (fp1 == NULL) {
  puts("cannot open this file");
  exit(0);
}

fp2 = fopen("output.txt", "w");
if (fp2 == NULL) {
  puts("Not able to open this file");
  fclose(fp1);
  exit(0);
}

do {
  a = fgetc(fp1);
  fputc(a, fp2);
} while (a != EOF);

fclose(fp1);
fclose(fp2);
getch();
}

我创建了文件 input.txt 和 output.txt,在我运行程序后,我没有看到该文本被复制。 (我也从 cmd 和直接从记事本创建 .txt 文件,但都没有工作)

【问题讨论】:

标签: c


【解决方案1】:

我建议进行以下更改。

建议 1

使用

int a;

而不是

char a;

取决于您的平台上char 的类型是签名还是未签名,

a = fgetc(fp1);

如果achar 类型可能会出现问题,因为fgetc 返回int

建议 2

do-while 循环存在缺陷。您最终会使用当前设置调用fputc(a, fp2),即使是a = EOF。将其更改为:

while ( (a = fgetc(fp1)) != EOF )
{
   fputc(a, fp2);
}

【讨论】:

    【解决方案2】:

    我尝试了你的代码,它可以工作,但是它向 output.txt 添加了垃圾值。所以你必须将 do-while 循环更改为 while 循环来解决这个问题。

    #include<stdio.h>
    #include<process.h>
    
    void main() {
        FILE *fp1, *fp2;
        int a;
    
        fp1 = fopen("input.txt", "r");
        if (fp1 == NULL) {
          puts("cannot open this file");
          exit(0);
        }
    
        fp2 = fopen("output.txt", "w");
        if (fp2 == NULL) {
          puts("Not able to open this file");
          fclose(fp1);
          exit(0);
        }
        while( (a = fgetc(fp1)) != EOF )
        {
          fputc(a, fp2);
        }
    
        fclose(fp1);
        fclose(fp2);
    }
    

    您也可以使用getcputc 代替fgetcfputc

    【讨论】:

    • 他怎么能用getcputc做到这一点?它们使用stdinstdout,而不是程序打开的文件。
    • EOF 是一个(有符号的)整数,因此它取决于char 的默认符号是否可以等于您的变量a。次要:如果“文本文件”包含一个字节 0xFF - 可能是 Unicode BOM 或 UTF8 序列的一部分 - 复制将提前停止。
    • @Barmar 你在说什么? getc 采用 FILE* putc 采用 intFILE*。我还用它们测试了代码,它有效。我同意应该将 a 声明为 int,但它仍然有效!跨度>
    • “它有效”是因为(我已经说过)您系统上的默认签名为 char。 (Barmar 可能将 getcfgetcgetchar 混淆了;我知道我这样做了,现在仍然这样做,直到我的编译器更正为止。)
    • @machine_1 是的,我把它和getchar 混淆了。
    【解决方案3】:

    此函数将第一个文件的内容复制到第二个文件。如果第二个文件存在 - 将覆盖它,否则 - 将创建它并将第一个文件的内容写入它。这个函数的关键是函数 fputc() (http://www.cplusplus.com/reference/cstdio/fputc/)。


    解决方案

    #include <stdio.h>
    
    /**
     * Copy content from one file to other file.
     */
    int
    copy_file(char path_to_read_file[], char path_to_write_file[])
    {
        char chr;
        FILE *stream_for_write, *stream_for_read;
    
        if ((stream_for_write = fopen(path_to_write_file, "w")) == NULL) {
            fprintf(stderr, "%s: %s\n", "Impossible to create a file", path_to_write_file);
            return -1;
        }
    
        if ((stream_for_read = fopen(path_to_read_file, "r")) == NULL) {
            fprintf(stderr, "%s: %s\n", "Impossible to read a file", path_to_read_file);
            return -1;
        }
    
        while ((chr = fgetc(stream_for_read)) != EOF)
            fputc(chr, stream_for_write);
    
        fclose(stream_for_write);
        fclose(stream_for_read);
    
        return 0;
    }
    

    用法:

    int
    main(int argc, char *argv[])
    {
        copy_file("file1", "file2");
        return 0;
    }
    

    【讨论】:

    • (chr = fgetc(stream_for_read)) != EOF。 EOF 实际上是一个-1,所以如果 chr 被签名(通常是这样),那么你可能会从文件中得到一些被视为 -1 的字节,因此是 EOF。
    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多