2015-02-09 61 views
-1

文件我有一些保存數據的ByteArray寫作的ByteArray在C

typedef struct { 
    uint32_t length; 
    uint8_t* bytes; 
} FREByteArray; 

在這裏,我想這個保存到一個文件

FREByteArray byteArray; 

if((fileToWrite = fopen(filePath, "wb+")) != NULL){ 
    fwrite(&byteArray.bytes, 1, byteArray.length, fileToWrite); 
    fclose(fileToWrite); 
} 

一個結構但這似乎沒有不保存所有數據,保存的文件大小爲16KB,實際數據大約爲32KB。我認爲fwrite不能將整個bytearray寫入文件。

這是保存ByteArray的正確方法嗎?一次調用中可以處理多少個fwrite?

+4

刪除'fwrite'行中的'&' – 2015-02-09 08:57:07

+0

'yteArray.bytes'是一個指針,不用'&'來再次獲取地址 – Gopi 2015-02-09 08:57:54

回答

3

更換

fwrite(&byteArray.bytes, 1, byteArray.length, fileToWrite); 

fwrite(byteArray.bytes, 1, byteArray.length, fileToWrite); 

以及由@Sourav戈什指出,確保byteArray.bytes指向正確的源位置。

+0

。假設'byteArray.bytes'正確地分配了內存。 :-) – 2015-02-09 08:59:16

+0

並假設byteArray.length保存正確的長度。 – vvvv 2015-02-09 09:26:15