[問題] fread讀binary檔 與 struct 問題
小弟有一段程式碼 搞不懂其中的問題
有一個檔案為binary檔 由以下struct存入
typedef struct _message
{
char command[10];
char status[10];
chat test[10];
} message;
其中資料類似以下
"aaaa bbbbb cccccc "
(由於是binary 所以空白並不等於空白)
主要程式碼 讀出binary檔案 使用相同的struct
void main (void)
{
FILE * fp = NULL;
message test;
memset(&test, 0, sizeof(_message));
fp = fopen("test","rb"); //binary file
if(fp != NULL)
{
int count = fread(&test,sizeof(test),1,fp);
printf("command : %s \n",test.command);
//"aaaa bbbbb cccccc "
//問題二:command 為何顯示出超過自己的大小,顯示出全部資料
printf("status : %s \n",status);
//"bbbbb cccccc "
//同上
printf("test : %s \n",test);
//"cccccc "
if(count) //count = 0 所以不會進到這個判斷式
{ //問題一 :fread 應該會返回讀到檔案的size ,但是這邊為何返回0
// ,明明有讀到資料??
printf("command : %s \n",test.command);
printf("status : %s \n",status);
printf("test : %s \n",test);
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.34.53.98
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1447052051.A.16F.html
推
11/09 14:57, , 1F
11/09 14:57, 1F
推
11/09 16:07, , 2F
11/09 16:07, 2F
推
11/09 22:25, , 3F
11/09 22:25, 3F
→
11/09 22:25, , 4F
11/09 22:25, 4F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章