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!

4
  • 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. Commented Mar 24, 2012 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 Commented Mar 24, 2012 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. Commented Mar 24, 2012 at 19:05
  • This is the data structure: typedef struct hash_record { char *hash; char *filename; } hash_record; Commented Mar 24, 2012 at 19:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd go for this. If you want to save strings you'll have to dump them in the file yourself.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.