如何在C语言中将一个文件的内容复制到另一个文件?

3
#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和notepad直接创建.txt文件,但都无效)


你是否参考了这个执行非常相似操作的链接?http://www.c4learn.com/c-programs/write-a-program-to-copy-the-contents-of-one-file-into-another-using-fputc.html - Jonnus
1
将void main(){}更改为**int main(void){}**。 - Michi
3个回答

4

I suggest the following changes.

Suggestion 1

Use

int a;

替代

char a;

根据您的平台上 char 的类型是有符号还是无符号的不同,

a = fgetc(fp1);

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

建议2

do-while循环有缺陷。在当前设置下,即使a = EOF,您仍将调用fputc(a,fp2)。请将其更改为:

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

1
我尝试了你的代码,它可以工作,但是它会向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


1
他如何使用 getcputc 进行操作?它们与程序打开的文件无关,而是与 stdinstdout 一起工作。 - Barmar
EOF是一个(有符号的)整数,因此它取决于char的默认符号性是否可以等于您的变量a。小问题:如果“文本文件”包含一个字节0xFF - 可能作为Unicode BOM或UTF8序列的一部分 - 复制将会过早停止。 - Jongware
@Barmar 你在说什么? getc 接受 FILE*putc 接受 intFILE*。我也用它们测试了代码,而且它可以工作。虽然我同意应该将 a 声明为 int,但它仍然可以工作! - machine_1
这是因为char在您的系统上默认是带符号的。 (Barmar可能混淆了getcfgetcgetchar; 我知道我做到了,直到被编译器更正之前。)“它起作用了”。 - Jongware
@machine_1 是的,我把它和 getchar 搞混了。 - Barmar

0

这个函数将第一个文件的内容复制到第二个文件。如果第二个文件存在,则覆盖它,否则创建它并将第一个文件的内容写入其中。此函数的关键是函数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的字节。 - aryndin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接