2017-04-06 36 views
0

我试图将一个恶意文件复制到一个文本文件。所以基本上我想把mal文件的内容复制到文本文件中。 mal文件名称为test1.mal,txt文件名称为output.txt。这是我的,但它保持打印出错读取文件。在C程序中复制文件?

#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; 
} 
+0

那是因为文件没有在当前目录中(你应该尝试使用'GETCWD()'打印) –

+0

'如果(FP1 == NULL || FP2 == NULL)'你确定? – Michi

+3

使用'perror'而不是'printf'来打印错误信息。这会告诉你**为什么前面的函数调用失败。另外,为每个'fopen'调用分别进行错误检查,以便知道哪个失败。 – dbush

回答

3

张贴的代码中有几个问题,其中许多在评论到OP的提问表示。

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

它完全编译并执行适当的错误检查

注:来电perror()将输出到stderr,封闭的文本和OS认为操作失败的原因。

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

#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 
+0

您可能应该将'O_CREAT'和'O_TRUNC'以及'mode'参数添加到'open(“output.txt”,O_WRONLY)'。也许'open(“output.txt”,O_WRONLY | O_CREAT | O_TRUNC,0666)''。是的,我知道 - 0666应该使用模式标志而不是魔术数字...... –

+0

@AndrewHenle,据我所知,如果文件已经存在或不存在,'O_WRONLY'将​​会处理,并且会截断现有的文件,所以不需要'O_TRUNC'。我必须承认,我很习惯让我的环境设置给予所有者和组的读写权限和世界读取权限,我甚至没有考虑过指定权限参数 – user3629249

+0

'O_WRONLY'意思是“仅供打印”,只有这一点。见http://pubs.opengroup.org/onlinepubs/9699919799/如果没有'O_CREAT','open()'不会创建文件。没有'O_TRUNC',它不会截断文件。 'open()'不像'fopen()'。 'fopen()'会根据传递的参数隐式地创建或截断文件,但是'open()'不会这么做。如果'open()'被传递'O_CREAT',则需要传递一个模式参数,因为如果文件被创建,'open()'将使用它找到的任何垃圾,其中'mode'参数应该作为新文件的权限。 –