0
的適當數量的,我無法理解下面的代碼片段,由碼例證的FREAD fwrite的行爲二進制模式很簡單:FWRITE在整數不寫字節
#include <stdio.h>
int main(void)
{
FILE *fp;
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int temp[100] = {0};
int i;
fp = fopen("testdata.bin","wb");
if(fp!= NULL) {
fwrite(arr,sizeof(int), 10, fp);
fclose(fp);
}
fp = fopen("testdata.bin","rb");
if(fp!= NULL) {
fread(temp,sizeof(int), 10, fp);
fclose(fp);
}
for(i=0;i<100;i++)
printf("%#x,",temp[i]);
printf("\b \n");
return 0;
}
在標準輸出上的輸出是:
0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
這是有道理的。但是,當我打開testdata.bin
文件時,我看到只有兩個字節值(int),我希望4字節的int值在我的機器上爲4。
這裏是testdata.bin的內容:
0x00000001: 01 00 02 00 03 00 04 00 05 00 06 00 07 00 08 00
0x00000010: 09 00 0a 00
我本來期望
0x00000001: 01 00 00 00 02 00 00 00 03 00 00 00 ...
任何想法?
您不會在代碼中報告程序是否能夠打開文件進行寫入。如果'fopen()'失敗,則不要在其中寫入。檢查來自'fopen()'的返回結果,並進行調試,報告其狀態。 –
請停止標記垃圾信息。這是C代碼,而不是C++。它們不是同一種語言,因爲它們都以* c *開頭,與* cat *和* car *相同,因爲它們以* ca *開頭。 –
@KenWhite它看起來像我的有效C++。 https://godbolt.org/g/4qQpFQ – xaxxon