2016-08-02 46 views
2

我試圖在新文件中複製文件,但它不起作用,因爲輸入是5133KB,輸出是614byte ......怎麼了?先謝謝你。複製C中的文件

#include <stdio.h> 

int main(void) 
{ 

    FILE * input = fopen("input.wav", "r"); 
    FILE * output = fopen("output.wav", "w"); 

    char buffer; 
    int  bytesRead = 1; 

    while(bytesRead=fread(&buffer,1,1,input)) 
    { 
     fwrite(&buffer,1,1,output); 
    } 

    fclose(input); 
    fclose(output); 

    return 0; 
} 
+1

*旁白*:除非您增加緩衝區的大小,否則程序會變得很不舒服。 –

+1

是的,我無法想象爲什麼你想一次讀一個字節。 –

+0

原因是我想寫最簡單的程序,我可以更好地顯示我的問題.. – thebesttony

回答

4

您可能需要在您的系統上二進制模式打開該文件。從C.2011,§ 7.21.5.3:

rb開放閱讀
wb截爲零或創建二進制文件寫入

所以二進制文件

FILE * input = fopen("input.wav", "rb"); 
FILE * output = fopen("output.wav", "wb"); 

原因是在某些系統上某些嵌入即使文件中實際存在更多字節,ded二進制字符也可能導致文本模式處理相信文件已結尾。