August 25th, 2013, 12:55 AM
Need help reading binary files
I am having problems reading a binary file with pointers in the struct, I can read the file when they are no pointers involved but for some reason I cant seem to read the file when I use pointers (I get errors when running). Any suggestions please let me know, I will leave my code down below.
I am sorry if I did not post the code correctly I am new here and did not have time to understand the forum to well (I usually post on another forum but it is currently down and this problem is really bugging me so quickly made an account and posted my problem here)
x = 0;
x2 = 0;
/*y = 0;
y2 = 0;*/
test = &x;
test2 = &x2;
/*complete = &y;
complete2 = &y2;*/
printf("\ntest : %2.2f\n", *test);
printf("test2: %2.2f\n\n", *test2);
printf("complete : %2.2f\n", *(end)->complete);
printf("complete2: %2.2f\n", *(end)->complete2);
void writeFile(float*test, float*test2)
fp = fopen( "savingValues.txt" , "wb" );
fwrite(test ,sizeof(test),1, fp );
void readFile(TEST *e)
fread(e->complete, sizeof(e->complete), 1, ptr_file);
fread(e->complete2,sizeof(e->complete2), 1, ptr_file);
August 25th, 2013, 01:55 AM
Here's something I wrote recently in response to a similar problem:
It is absolutely useless to save pointers to a file. Rather you need to save the data and the structure of the data to the file to be able to reconstruct that structure when you read it in from the file. That is called data serialization, also "marshalling".
But as useless and foolhardy the idea of writing pointers to a file may be, the actual problem that you are encountering is that end is an uninitialized pointer. It contains garbage, which means it could be pointing anywhere. It might help if you were to think of a pointer as a loaded gun. Using an uninitialized pointer is like being in a room filled with people who mean very much to you and blindly pointing that loaded gun in a completely random direction and firing it. Nobody in their right mind would even consider doing something like that, and yet you just did and you don't understand why it isn't a good idea. In purely computer terms, the OS gave your program a segment of its total memory space; you can access any memory location in your own memory space, but you are forbidden to access any location outside that space, since the rest of the memory belongs to other programs and to the operating system. An uninitialized pointer will more likely than not try to access outside your memory space. Even if you just happen to access inside your own memory space, you are likely to try to access read-only memory. And even if you can access read/write memory inside your own memory space, you will probably overwrite other data, causing "inexplicable behavior."
malloc some memory to that pointer before you try to use it.
And writing and reading pointers to and from files is a dumb idea.
August 25th, 2013, 02:34 AM
I was thinking of initializing those pointers but when looking at some of my older codes I noticed that I never done that when reading a file and the program worked with no problems, but I will see if initializing it would help.
August 25th, 2013, 02:44 AM
So I initialized and it worked thanks
Time for me to fix my main program :D