C中的复制文件功能

CaT*_*aTx 3 c fopen copy file stream

我尝试使用此功能复制文件,但输出文件包含奇怪的字符.

int File_Copy (char FileSource [], char FileDestination [])
{
    int     result      =   -1;
    char    c [1];
    FILE    *stream_R   =   fopen (FileSource,      "r");
    FILE    *stream_W   =   fopen (FileDestination, "w");   //create and write to file

    while ((c [0] = (char) fgetc(stream_R)) != EOF)
    {
        fprintf (stream_W, c);
    }

    //close streams
    fclose  (stream_R);
    fclose  (stream_W);

    return result;
}
Run Code Online (Sandbox Code Playgroud)

我不知道出了什么问题.请帮忙.

Enn*_*nno 7

您尝试一次复制一个字节有什么原因吗?那会很慢!尽管您的主要问题可能是您使用 fprintf(),并且 printf() 函数用于打印格式化字符串,而不是单个字符。

如果您只是将字节从一个文件推送到另一个文件,那么您应该使用 fread 和 fwrite 来代替,如下所示:

int File_Copy(char FileSource[], char FileDestination[])
{
    char    c[4096]; // or any other constant you like
    FILE    *stream_R = fopen(FileSource, "r");
    FILE    *stream_W = fopen(FileDestination, "w");   //create and write to file

    while (!feof(stream_R)) {
        size_t bytes = fread(c, 1, sizeof(c), stream_R);
        if (bytes) {
            fwrite(c, 1, bytes, stream_W);
        }
    }

    //close streams
    fclose(stream_R);
    fclose(stream_W);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Iha*_*imi 6

问题是c[1]不能作为字符串工作,因为它不能包含终止nul字节,所以它应该是

char c[2] = {0};
Run Code Online (Sandbox Code Playgroud)

c[2]应该int像这样

int c[2] = {0};
Run Code Online (Sandbox Code Playgroud)

因为fgetc()返回int所以你的代码可能会溢出c[0],但你也可以改进其他一些东西.

  1. 你不需要c成为一个数组,你可以像这样声明它.

    int c;
    
    Run Code Online (Sandbox Code Playgroud)

    然后用fputc();而不是fprintf().

  2. 您必须检查没有任何fopen()调用失败,否则您的程序将因NULL指针取消引用而调用未定义的行为.

这是您自己的程序的强大版本,您修复了问题中描述的问题

/*   ** Function return value meaning
 * -1 cannot open source file 
 * -2 cannot open destination file
 * 0 Success
 */
int File_Copy (char FileSource [], char FileDestination [])
{
    int   c;
    FILE *stream_R;
    FILE *stream_W; 

    stream_R = fopen (FileSource, "r");
    if (stream_R == NULL)
        return -1;
    stream_W = fopen (FileDestination, "w");   //create and write to file
    if (stream_W == NULL)
     {
        fclose (stream_R);
        return -2;
     }    
    while ((c = fgetc(stream_R)) != EOF)
        fputc (c, stream_W);
    fclose (stream_R);
    fclose (stream_W);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)