【问题标题】:copying files in C program?在C程序中复制文件?
【发布时间】:2017-09-02 02:00:01
【问题描述】:

我正在尝试将恶意文件复制到文本文件。所以基本上我希望将mal文件的内容复制到文本文件中。 mal 文件名称为 test1.mal,txt 文件名称为 output.txt。这就是我所拥有的,但它不断打印出读取文件的错误。

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



int main(void) {

char content[255];
char newcontent[255];

FILE *fp1, *fp2;
fp1 = fopen("test1.mal", "r");
fp2 = fopen("output.txt", "w");

if(fp1 == NULL || fp2 == NULL)
{
printf("error reading file\n");
exit(0);
}
printf("files open correct\n");
while(fgets(content, sizeof (content), fp1) !=NULL)
{
fputs(content, stdout);
strcpy (content, newcontent);
}

printf("%s", newcontent);
printf("text received\n");

while(fgets(content, sizeof(content), fp1) !=NULL)
{
fprintf(fp2, newcontent);
}
printf("file created and text copied");

fclose(fp1);
fclose(fp2);
return 0;
}

【问题讨论】:

  • 这是因为文件在当前目录中不存在(你应该尝试使用getcwd()打印)
  • if(fp1 == NULL || fp2 == NULL)你确定吗?
  • 使用perror 而不是printf 来打印错误信息。这将告诉您为什么前面的函数调用失败。另外,对每个 fopen 调用分别进行错误检查,以便知道哪个失败。
  • 检查这一行 ==>> fprintf(fp2, newcontent) 也。
  • 如果制作fopen 的人能够提供一些方法来找出为什么它失败了。

标签: c file file-copying


【解决方案1】:

贴出的代码有几个问题,很多问题都在cmets中对OP的问题表示出来。

以下代码是执行所需操作的一种方式。

它干净地编译并执行适当的错误检查

注意:对perror() 的调用将向stderr 输出随附的文本以及操作系统认为操作失败的原因。

注意:使用open()close()read()write(),因为不能保证输入的 .mal 文件不包含嵌入的 NUL 字符。

#include <stdio.h>    // perror()
#include <stdlib.h>   // exit(), EXIT_FAILURE


#include <unistd.h>   // read(), write(), close()
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>    // open()

// declare the size of the buffers with a meaningful name
// do not use 'magic' numbers
#define BUFF_SIZE 255

int main(void)
{

    char content[ BUFF_SIZE ];

    int fin;
    int fout;

    if( 0 > (fin = open("test1.mal", O_RDONLY) ) )
    {
        perror( "open for read of test1.mal failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, open successful

    if( 0 > (fout = open("output.txt", O_WRONLY) ) )
    {
        perror( "open for write of output.txt failed");
        close( fin );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    printf("files open correct\n");

    ssize_t readCount;
    while( 0 < (readCount = read( fin, content, sizeof( content) ) ) )
    {
        //fputs(content, stdout);  // are you sure the file contents are printable?
        if( readCount != write( fout, content, (size_t)readCount ) )
        { // then write error occured
            perror( "write of data to output file failed" );
            close( fin );
            close( fout );
            exit( EXIT_FAILURE );
        }

        // implied else, write successful
    }

    if( 0 > readCount )
    { // then read error occurred
        perror( "read of file failed" );
        close( fin );
        close( fout );
        exit( EXIT_FAILURE );
    }

    // implied else, complete file copied

    printf("file created and text copied\n");

    close( fin );
    close( fout );
    return 0;
} // end function: main

【讨论】:

  • 您可能应该将O_CREATO_TRUNC 以及mode 参数添加到open("output.txt", O_WRONLY)。也许open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666)。是的,我知道 - 0666 应该使用模式标志而不是幻数...
  • @AndrewHenle,据我了解,O_WRONLY 将处理文件是否已存在并将截断现有文件,因此不需要O_TRUNC。我必须承认我已经习惯了让我的环境设置为所有者和组授予读写权限以及世界读取权限,以至于我什至没有考虑指定权限参数
  • O_WRONLY 的意思是“只对写作开放”,仅此而已。见pubs.opengroup.org/onlinepubs/9699919799 没有O_CREATopen() 将不会创建文件。如果没有O_TRUNC,它不会截断文件。 open() 不像 fopen()fopen() 将根据传递的参数隐式创建或截断文件,但 open() 不会那样做。如果open() 被传递O_CREAT,你需要传递一个模式参数,因为如果文件被创建,open() 将使用它找到的任何垃圾,mode 参数应该是新文件的权限。跨度>
  • @AndrewHenle,很高兴知道,感谢您的澄清
猜你喜欢
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
相关资源
最近更新 更多