Thus, I am having some problem of being unable to read the binary in my structure correctly. The structure is as follows:
struct Student { char name[25]; int quiz1; int quiz2; int quiz3; };
This is 37 bytes (25 bytes from the char array and 4 bytes per integer). My .dat file is 185 bytes. These are 5 students with 3 whole classes. Therefore, each student takes 37 bytes (37 * 5 = 185).
In text format, it looks something like this:
Bart Simpson 75 65 70 Ralph Wiggum 35 60 44 Lisa Simpson 100 98 91 Martin Prince 99 98 99 Milhouse Van Houten 80 87 79
I can read each entry separately with this code:
Student stud; fstream file; file.open("quizzes.dat", ios::in | ios::out | ios::binary); if (file.fail()) { cout << "ERROR: Cannot open the file..." << endl; exit(0); } file.read(stud.name, sizeof(stud.name)); file.read(reinterpret_cast<char *>(&stud.quiz1), sizeof(stud.quiz1)); file.read(reinterpret_cast<char *>(&stud.quiz2), sizeof(stud.quiz2)); file.read(reinterpret_cast<char *>(&stud.quiz3), sizeof(stud.quiz3)); while(!file.eof()) { cout << left << setw(25) << stud.name << setw(5) << stud.quiz1 << setw(5) << stud.quiz2 << setw(5) << stud.quiz3 << endl;
And I get a nice conclusion, but I want to be able to read one whole structure at a time, and not just individual members of each structure at a time. This code is what, in my opinion, is necessary to complete the task, but ... it does not work (after that I will show the result):
*, not counting similar parts, like opening a file declaration and structure, etc.
file.read(reinterpret_cast<char *>(&stud), sizeof(stud)); while(!file.eof()) { cout << left << setw(25) << stud.name << setw(5) << stud.quiz1 << setw(5) << stud.quiz2 << setw(5) << stud.quiz3 << endl; file.read(reinterpret_cast<char *>(&stud), sizeof(stud)); }
OUTPUT:
Bart Simpson 16640179201818317312 ph Wiggum 288358417665884161394631027 impson 129184563217692391371917853806 ince 175193530917020655191851872800
The only part that she did not spoil is the first name, after which she went down the hill. I tried everything and I do not know what happened. I even looked through the books that I have, and could not find anything. Things there are similar to what I have, and they work, but for some odd reason mine doesnβt. I did file.get (ch) (ch being a char) in byte 25, and it returned K, which is ASCII for 75 .., which is the first test score, so that's where it should be. He just doesn't read my structures properly.
Any help would be greatly appreciated, I was just stuck with this.
EDIT: After getting so many unexpected and amazing input from you guys, I decided to take your advice and stick to reading in one member at a time. I did things cleaner and less using functions. Thanks again for providing such quick and enlightening material. It is very much appreciated.
If you are interested in a workaround that is not recommended by most, scroll down to the 3rd answer by user 1654209. This workaround works flawlessly, but read through all the comments to see why it is not approved.