Thursday, October 15, 2009

How to read Binary Data File in C (Sequentially)

Exactly two lines have to be changed from ASCII file mode given here.
Differences from ASCII mode:
1 - Additional "b" for fopen()
2 - Usage of fread() instate of fscanf
*/
...
fp = fopen(i_filename, "rb"); // read a binary file ("r" and "b")
...
/* loop until all data keep into array */

while( (!feof(fp))){
//fscanf(fp, "%f", &value); you can not use this any more for binary files. Now you have have to use fread()
fread(&value, sizeof(FP32), 1, fp);
...
}

Labels: , ,