简体   繁体   中英

Reading from binary file in C after saving

I'm trying to read the binary file that I created. The printing is not working, and it's printing number set (354), that's not even in the file. I'd appreciate help to my problem.

#include <stdio.h>
#include <stdlib.h>

int test();

int main(void) {

FILE *f;

    f = fopen("nums.bin", "wb");

    srand(40); 

    for(int i = 0; i<20; i++) 
        fprintf(f, "%d ", rand()%1000); 
    printf("Numbers saved to file.\n");
    fclose(f);

    test();
    return 0;
}

int test() {

FILE *f;
int i=0;
    printf("The numbers in the file are...\n");
    f = fopen("nums.bin", "rb");

    fread(&i, sizeof(i), 2, f);
    printf("%d", rand()%1000);
return 0;
}

Everything else works as intended (numbers in file are as I want them to be etc.). There's just something wrong with the printing out of the file. Thank you

you write number as text :

    fprintf(f, "%d ", rand()%1000); 

but you read number as binary

fread(&i, sizeof(i), 1, f);

this is not compatible.

If you write with that fprintf you have to read using fscanf or equivalent with the format "%d" like when you written.

Else to read doing fread(&i, sizeof(i), 1, f); you have to write like that :

int n = rand()%1000;

fwrite(&n, sizeof(n), 1, f);

out of that, something strange in your code :

printf("The numbers in the file are...\n");
...
fread(&i, sizeof(i), 2, f);
printf("%d", rand()%1000);

so you read a number (whatever the way) but you don't print it, you print a random value, why do you not print i ?

After printf("The numbers in the file are...\\n"); it seems logic to a for similar to the one in main to read values from file and print them on stdout


A proposal to write/read in binary :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void test();

int main(void)
{
  FILE *f = fopen("nums.bin", "wb");

  if (f == 0) {
    puts("cannot open file to write in");
    return -1;
  }

  srand(time(NULL)); /* practical way to have different values each time the program runs */

  for (int i = 0; i<20; i++){
    int n =  rand()%1000; 

    printf("%d ", n); /* to check the read later */
    fwrite(&n, sizeof(n), 1, f);
  }
  printf(" are saved to file.\n");
  fclose(f);

  test();
  return 0;
}

void test() {
  FILE *f = fopen("nums.bin", "rb");

  if (f == 0) {
    puts("cannot open file to read in");
    return;
  }

  printf("The numbers in the file are :\n");

  for (int i = 0; i<20; i++){
    int n;

    fread(&n, sizeof(n), 1, f);
    printf("%d ", n);
  }

  putchar('\n');
  fclose(f);
}

Example (values change each time) :

pi@raspberrypi:/tmp $ gcc -pedantic -Wall r.c
pi@raspberrypi:/tmp $ ./a.out
208 177 118 678 9 692 14 800 306 629 135 84 831 737 514 328 133 969 702 382  are saved to file.
The numbers in the file are :
208 177 118 678 9 692 14 800 306 629 135 84 831 737 514 328 133 969 702 382 

Your randomization initialization srand(40) will have no effect on the quality of your random numbers. You should usually use something like srand(time(null)) to get something that is more "random".

Your output at the end of test is printing a random number, not the integer you've read before. Also, you're reading two integers in the line fread(&i, sizeof(i), 2, f); which will corrupt your stack.

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