1

I'm experiencing some problems while trying to read a binary file in C. This problem never happened to me before, I don't really know how to manage it.

So, there's this structure called "hash_record", there are many of them stored in my "HASH_FILE" file in binary mode. This is the structure:

typedef struct hash_record {
char *hash;
char *filename;
} hash_record;

I write the file in this way:

hash_record hrec;
[...] code that fill the structure's fields [...]
FILE* hash_file = fopen(HASH_FILE, "ab");
fwrite(&hrec, sizeof(hash_record), 1, hash_file);
fclose(shared_file);

This is just a summary, the fwrite() function is inside a loop so that I can fill the file with many hash_record's. Then, immediately after that piece of code, I start reading the file and printing some data to be sure everything went well. This is the code:

int print_data() {
hash_record rec;

printf("Data:\n");
FILE* hash_file = fopen("hash.bin", "rb");
if (hash_file == NULL)
    return -1;
while(fread(&rec, sizeof(hash_record), 1, hash_file) == 1)
    printf("Filename: %s - Hash: %s", rec.filename, rec.hash);
fclose(hash_file);
return 0;
}

And everything works just fine! The problem is that if I write the binary file in an instance of my program and then quit it, when I open it again (commenting the code which write the file so it can only read it) it gives me a Segmentation Fault. This error appears when I call the printf() inside the while() loop. If I just print a common string without calling "rec" no errors are given, so I'm assuming there's something wrong storing data inside "rec".

Any idea?

Thank you!

Jack
  • 97
  • 1
  • 10
  • 3
    You cannot serialize a `char*`, at least not in the way you think. You need to do serialization properly, something which goes beyond the scope of a comment. – Kerrek SB Mar 24 '12 at 19:03
  • I'm guessing the `fread` call is not storing anything in `rec`, which is why the printf fails. Use a debugger to make sure `rec` is not null – prelic Mar 24 '12 at 19:04
  • 2
    We're going to need more information to help you. Some specifics about the data structure you are writing. In particular, does the structure contain addresses? If so, the address in the writing program is irrelevant to the reading program; you must re-establish the address. However, what you show does not tell us what the problem is. – Jonathan Leffler Mar 24 '12 at 19:05
  • This is the data structure: typedef struct hash_record { char *hash; char *filename; } hash_record; – Jack Mar 24 '12 at 19:15

1 Answers1

4

You are writing out pointers. When you read them back in from the same instance of the program, the data is in the same place and the pointers are meaningful. If you read them in from another instance, the pointers are bad.

  • I'd go for this. If you want to save strings you'll have to dump them in the file yourself. – Guido Mar 24 '12 at 19:22
  • Thank you! You are right, the problem was in the structure itself. I used pointers to store strings and I didn't think at it when writing everything to file. – Jack Mar 24 '12 at 19:25