C++ Binary Files Problem

This is a discussion on C++ Binary Files Problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to create a program that has a class called student which holds a name, age and gpa. ...

C++ Binary Files Problem

Hi, I'm trying to create a program that has a class called student which holds a name, age and gpa. Then in my main I want to create an array of student pointers. Then I want the user to be able to create students and store them into the array. Then when they exit the program it will save the array to a binary file. Then when the program is started again the binary file is read back into an array. I have everything working except the read and or write part of the binary file. When I try printing the array after I have read the file back into the array I either get results like name=nothing age=-432432 and gpa=432432 or the program spits out a bunch of garbage then crashes. Here are both functions...

means array is an array of pointers to type student. The amount of memory necessary to hold a pointer may or may not be the same as the amount of memory necessary to hold information for an object of type student, yet that is what you are asking for with sizeof(student).

Writing pointers to file won't work well, either, as you will be writing an address to file, not what exists at that address, to file. Who know what will be at that memory address at a later time when you read back the address from file.

I'd suggest trying with student array[] as the first parameter, instead of student * array[], if at all possible.

The difference was the first one would crash the program when I would try to print the array after reruning the program and the second one wouldn't crash the program but would print huge numbers in place of the variables. ex. Name should be Jane age should be 20 and gpa should be 3.4, however would print out name is nothing, age is 3234324324, and gpa is 54333656. Thank you for your time.

Edit: This is the declaration... student * arr[10];
However, these use to not be in functions, just part of main and I still had this problem so it's not to do with how I pass it to the function.

Is the 'name' member of your student struct a pointer itself, i.e. a pointer to char representing a character array where the name is stored for you student? Or, is it just a simple character array?

If it is a pointer then you might get weird numbers if you write/read a pointer to the file since you would be writting/reading an address and not the data you think. You would need to handle this a certain way, for example your output routine would need to write a length and then output a number of bytes equal to this length representing the name. After this you would then write the age and gpa data. Your input routine would need to read this length back in first, then allocate memory to your 'name' pointer of this length, and then read in bytes from the file equal to this length into the 'name' field followed by reading in the age and gpa bytes.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens

> while(!in.eof())
Read the FAQ
This is notorious for going round the loop one too many times, which if you're reading in just 10 students means you critically make the mistake of allocating the element just past the end of the array.

The hacky x--; suggests that this is actually happening, as your fix to the off-by-one error which would otherwise be present.

The above creates a Student1 object, and assigns it's address to an element of s1array which is an array of Student1 pointers. The array element is then written to file in binary fashion, and read from file to a different object of the Student1 class which is then displayed on the screen to confirm that the copying, writing, reading, etc. all works out. The program.....

(whatever the problem is can be explained here---this program seems to compile and work appropriately for me)

Providing this type of information from your program, without all the extraneous material, would be helpful to those willing to look, and try to help.

Thanks for posting the question. Working through it so far has helped me better understand the process better, too.

Forgive me for not re-reading the thread of downloading the zip file, but you cannot write a structure to a binary file using read and write like that if the struct contains members with complex structures. In this case, the string class most likely holds a pointer to the string data held elsewhere in memory. That data will not be written to the binary file, only the pointer to it will be written. Of course, when you read tha data back in, the pointer will be invalid, so it won't work.

If you want to write a struct with a string member to a binary file, you will have to have a special function that specifically writes out each piece of data appropriately (and another for reading). That function should probably write the size of the string followed by the result of a call to c_str() on the string member(s), so that the read function can read in the correct number of bytes for the string the next time through.

Here's a version of my previous program using std::string for name instead of char[]. In this case using char[] instead of std::string might be worth it, although I'd probably avoid writing to binary file in first place.

Hopefully, I didn't erase a brace or semicolon or & when I edited out comments etc before copy and pasting this version from my compiler.

I couldn't get it to work with passing an array of pointers to students to the binary file, so I had an object equal what the pointer was pointing to then I passed the object. However, I still couldn't get it to work with a string. So I changed it to an array of chars, however, that still didn't work so I just scrapped the name. If anyone knows how you can pass an object of a class that contains a string to a binary file, I would like to know. Thank you all for your help.

This works on Dev-C++ though I couldn't get it to work on VC6 with similar syntax the other night when I was working on this. I'll retry later with VC6 to see if I screwed up somehow earlier (not an impossibility), or if there's a difference between VC6 and DevC++.