无法将一个文件复制到另一个文件

时间:2015-03-12 16:11:28

标签: c

虽然我正在从旧书中学习C(可能是问题),但我编写了代码来将一个文件的内容复制到另一个文件中。 但不知何故,程序停止工作。我将不胜感激。

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
    FILE *fin, *fout;       //Pointers to the files
    int ch;

    if (argc!=3)    //Just checking if the user inserted the correct information
    {
        printf("\nCorrect mode: Program name, file1 -> file2 \n\n");
        exit(1);
    }
    fin=fopen(argv[1], "rb");

    if (fin==NULL)      //Checking if the file exists
    {
        printf("\n\nERROR!\n\nThe file you're trying to open does not exist or it cannot be opened.\n\n");
        exit(2);
    }

    if ((fout=fopen(argv[2], "wb"))==NULL)  // If it cannot create a file
    {
        printf("\n\nERROR!\n\nImpossible to create the file %s\n\n", argv[2]);
        exit(3);
    }

    while ((ch=fgetc(fin))!=EOF)
        fputs(ch, fout);

    fclose(fin);
    fclose(fout);
}

1 个答案:

答案 0 :(得分:4)

您正在使用fputs来撰写字符。它用于字符串(char数组)。而是使用fputc