How do I read a binary file line by line and store the contents in C
I have a binary file called add_num.mi.
Now I am able to inspect the contents of the file by running the command
xxd -c 4 add_nums.mi | head -n 9
Now this contains the following binary information
00000000: 1301 f07f ....
00000004: ef00 c000 ....
00000008: b717 0000 ....
0000000c: 2386 0780 #...
00000010: b717 0000 ....
00000014: 1307 8004 ....
00000018: 2380 e780 #...
0000001c: 1305 0000 ....
00000020: 6780 0000 g...
How would I print this out in C code programmatically.
At the moment this is the attempt I have made but it does not work correctly.
#include <stdio.h>
int main() {
FILE *fp;
unsigned char ch;
fp = fopen("examples/add_2_numbers/add_2_numbers.mi", "rb");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF) { // read file character by character
printf("%c", ch);
}
fclose(fp);
return 0;
}