[問題] fread讀binary檔 與 struct 問題

看板C_and_CPP (C/C++)作者 (kuangs)時間10年前 (2015/11/09 14:54), 編輯推噓3(301)
留言4則, 3人參與, 最新討論串1/1
小弟有一段程式碼 搞不懂其中的問題 有一個檔案為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
印出message的16進位來看看
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
文章代碼(AID): #1MG4CJ5l (C_and_CPP)
文章代碼(AID): #1MG4CJ5l (C_and_CPP)