2017-04-06 38 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'參數應該作爲新文件的權限。 –