看板 C_and_CPP 關於我們 聯絡資訊
小弟有一段程式碼 搞不懂其中的問題 有一個檔案為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
LPH66: 那個二進位檔是如何產生的? 11/09 14:57
stupid0319: 印出message的16進位來看看 11/09 16:07
johnpage: 二進制 與 字串 是同一種變數型態嗎? 11/09 22:25
johnpage: 用字串函數顯示二進制資料????? 11/09 22:25