so I have this problem, I want to write array of numbers to binary file and then read them from it and write them into another array, but I get some wrong numbers, how do i fix it ?
Code:
#include <stdio.h>#include <conio.h>
int main() {
int num_arr[9]; int m = 0;
FILE *fp;
fp=fopen("test.bin", "rb");
int x[10] = { -1,-3,-2,-5,-6,-7,-8,-9,-4 };
for (int i = 0; i <= 9; i++) {
fwrite(&x[i], sizeof(int), 9, fp);
}
for (int i = 0; i <= 9; i++) {
fread(&m, sizeof(int), 9, fp);
num_arr[i] = m;
printf("%d\n ", num_arr[i]);
}
printf("\n");
int y = 0, k = 0, z = 0;
for (int j = 0; j <= 9; j++) {
if (y < num_arr[j])
y = num_arr[j];
if (k < num_arr[j] && num_arr[j] < y)
k = num_arr[j];
if (z < num_arr[j] && num_arr[j] < y && num_arr[j] < k)
z = num_arr[j];
}
printf("Three max nums in array: \n%d\n%d\n%d", y, k, z);
getch();
}