-
Binary file output
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();
}
-
You need a rewind() or an fseek() between the fwrite(), and fread().
Please do not use conio.h and getch(). They are specific to some Windows based compilers, and are NOT Standard C!!!
-
can you explain a bit please ?
-
Code:
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);
}
rewind(fp);
fseek(fp, sizeof(int), SEEK_SET);
for (int i = 0; i <= 9; i++) {
fread(&m, sizeof(int), 9, fp);
num_arr[i] = m;
printf("%d\n ", num_arr[i]);
}
is it ok?
but it doesn`t work
-
I said you need a rewind() OR an fseek(), but NOT both!!!
Remove line 8, the fseek(). A rewind in your case is fine.
Two reasons for one of these functions. Whenever you write to a file opened as you have, the read from it, you need to flush the buffers. You do this with a rewind() or an fseek().
In your case you have written 9 ints, probably 36 bytes into the file. In order to read the data you have written to the file, you need to reset the position in the file back to 0, or the first byte in the file. You are doing that with a rewind().
Also, I didn't catch it before, since I did not compile the program, if you are using a for() loop 9 times to write and read the data, you need to specify 1 int not 9:
Code:
fwrite(&x[i], sizeof(int), 9, fp);
// Should read:
fwrite(&x[i], sizeof(int), 1, fp);
// The same for the fread()
You also should check the return value from fopen() to insure that the file was opened, fwrite() to insure data has been written, and fread() to insure you have read the data you expect to read.
Start with this and if you still have problems, please repost your whole program, and I will compile and test it.
Please make sure your warning level in your compiler is set to the highest level.
-
What type of variable is m? What exactly are you trying to read into this variable?
-
Quote:
Originally Posted by
wztch
Code:
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);
}
You write in "x[]" not in num_arr[]. So, in num_arr[] is nothing.
Code:
Code:
rewind(fp);
fseek(fp, sizeof(int), SEEK_SET);
Not both.
For example:
Code:
. . .
int num_arr[10]; //For nine numbers
int result;
. . .
//For read
//File pointer to the beginning
result = fseek(fp, 0L, SEEK_SET);
if (result)
{
//If result is not 0; -1 would be an error
perror("fseek not possible");
}
else
{
fgets(num_arr, 9, fp);
printf("%s", num_arr);
}
-
Look closely at your read() and write() calls. What exactly is the purpose of the third parameter?
You may also want to take a look at your file with a hex editor/dump program to see exactly what you actually wrote to the file.