简体   繁体   中英

Reading raw data file, getting different values in Matlab and C++

I'm converting some code from Matlab (which I'm not familiar with) to C++. Part of the code reads in a raw data file and inputs every 8 bits into an element of a matrix.

MATLAB:

header=fread(fid, 512, '*uint8');

Similarly, in C++ I have:

fread(&q1[0][0], sizeof(uint8_t), 512, filepath);

These are both reading the same file of course, and the values they spit out match until element #33.

10
0
0
0
244
1
0
0
10
0
0
0
244
1
0
0
10
0
0
0
244
1
0
0
10
0
0
0
208
7
0
0

Then, my C++ program spits out "92" while my Matlab code spits out 180. Values start diverging:

C++ / MATLAB
92 / 180
58 / 118
230 / 219
60 / 133

and keep going on without matching for the rest of the 512 bytes.

Any ideas as to what could be causing this?

Did you open the file in C++ with the 'b' option for binary files? The default it 't', text mode and it will intepret the line feed/carriage returns differently.

Edit: assuming this is Windows. If not Windows then you can ignore this.

Turns out, Matlab was pre-processing the raw file that I was sending in, which changed the headers. Although the files were "copies" (as far as windows was concerned), their headers were different giving me different values when printing out the beginning of the raw data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM