[問題] fread讀binary file的問題

看板C_and_CPP (C/C++)作者 (蟲聲)時間12年前 (2013/10/11 14:45), 編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
不好意思問題應該很蠢 卡一陣子了 想讀一個存有16-bit unsigned integer 的bin檔 用matlab可以簡單的讀到 : fid = fopen('FILENAME.bin','rb'); frd = fread(fid,'int16'); frd 就存了一個矩陣 用C的話一直弄不好 #include <stdio.h> #include <stdlib.h> #define N 27 int main() { unsigned short get[N]; int i; FILE *file = fopen("FILENAME.bin","rb"); fread(get, sizeof(unsigned short),N,file); for ( i=0; i < 27; i++ ) { printf("%x\n",get[i]); } fclose(file); system("pause"); } 是有值存進get 不過長度跟值都不對 請問怎麼弄成跟matlab一樣呢... ? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.114.27.154 已解決 ※ 編輯: REer 來自: 140.114.27.154 (10/11 16:22)

10/12 02:16, , 1F
解決方法是?
10/12 02:16, 1F

10/12 11:51, , 2F
因為資料是 16-bit int 所以把 fread 從 unsigned char
10/12 11:51, 2F

10/12 11:51, , 3F
改成 unsigned short int 就可以正常讀到數字了...
10/12 11:51, 3F

10/12 11:53, , 4F
現在的 code 已修正所以看不出問題, 但原本的 code 因為
10/12 11:53, 4F

10/12 11:53, , 5F
data type寫錯,有很明顯的違和感,原PO自己很快就發現了
10/12 11:53, 5F
文章代碼(AID): #1ILvvpjV (C_and_CPP)
文章代碼(AID): #1ILvvpjV (C_and_CPP)