Reading Binary File into a Structure (C++) -
So I have an issue in my composition to not properly read a binary file. The structure is:
Structure Student {Four names [25]; Int quiz 1; Int quiz 2; Int quiz 3; }; This is 37 bytes (25 bytes from four arrays and 4 bytes in integer). My .at file is 185 bytes. I have 5 students who have this 3 integer grade. Therefore each student takes 37 bytes (37 * 5 = 185).
Bit Simpson 75 65 70 Ralph Vigam 35 60 44 Lisa Simpson 100 98 91 Martin Prince 99 98 99 Mallhaus Van Hauten 80 87 79
I can read each record individually using this code:
student studs; Fstream file; File.open ("quizzes.dat", iOS :: in | iOS :: Out | iOS :: Binary); If (file.fail ()) {cout & lt; & Lt; "Error: Can not open file ..." & lt; & Lt; Endl; Exit (0); } File.read (stud.name, sizeof (stud.name)); File.read (reinterpret_cast & lt; char * & gt; (& stud.quiz1), sizeof (stud.quiz1)); File.read (reinterpret_cast & lt; char * & gt; (and stud.quiz2), size (stud.quiz2)); File.read (reinterpret_cast & lt; char * & gt; (& stud.quiz3), sizeof (stud.quiz3)); While (! File.eof ()) {cout & lt; & Lt; Left & lt; & Lt; Setup (25) & lt; & Lt; Stud.name & lt; & Lt; Setup (5) & lt; & Lt; Stud.quiz1 & lt; & Lt; Setup (5) & lt; & Lt; Stud.quiz2 & lt; & Lt; Setup (5) & lt; & Lt; Stud.quiz3 & lt; & Lt; Endl; // read the next record file. Read (stud.name, sizeof (stud.name)); File.read (reinterpret_cast & lt; char * & gt; (& stud.quiz1), sizeof (stud.quiz1)); File.read (reinterpret_cast & lt; char * & gt; (and stud.quiz2), size (stud.quiz2)); File.read (reinterpret_cast & lt; char * & gt; (& stud.quiz3), sizeof (stud.quiz3)); } And I'm getting a good looking output, but I want to be able to read one whole frame at a time, not only individual members of each structure at a time. This is the code that I believe is necessary to complete the work, but ... it does not work (I will show the output after that):
* File and structure declaration, etc.
file.read (reinterpret_cast & lt; four * & gt; (more studs), size (studs)); While (! File.eof ()) {cout & lt; & Lt; Left & lt; & Lt; Setup (25) & lt; & Lt; Stud.name & lt; & Lt; Setup (5) & lt; & Lt; Stud.quiz1 & lt; & Lt; Setup (5) & lt; & Lt; Stud.quiz2 & lt; & Lt; Setup (5) & lt; & Lt; Stud.quiz3 & lt; & Lt; Endl; File.read (reinterpret_cast & lt; char * & gt; (more studs), size (studs)); } Output:
Bart Simpson 16640179201818317312 pH viggum 288358417665884161394631027 Impson 12 9184563217692391371917853806 Ace 175193530917020655191851872800 Not the only part It's not a mess, first name, after that it is down the hill. I have tried everything and I do not know what is wrong. I have searched through books and I have not got anything. There are things that I have and they work, but for some strange reasons, I do not have mine. I did file.get (ch) (four four by one) and it returned to Kashmir, which is the ASCI for 75 .. which is the first test score, therefore, where everything should be. It is not reading properly in my structures.
Any help would be greatly appreciated, I have just been left with this one.
Edit: After you receive such a large amount of unexpected and awesome input from the people, I have decided to take your advice and read one member at a time Have been stick with. I used to work cleaner and made small things Thank you once again to provide such quick and informative input. It has been greatly appreciated. If you are interested which is not recommended by most, then scroll downward, answering user1654209 to answer 3. This solution works flawlessly at any time, but read all comments to see why it has not been appreciated.
Definitely it has been padded to protect the alignment of its content, that means it 37 bytes will not be there, and that mismatch causes the reason for reading out of sync. The way in which each string is losing 3 characters, it looks like it has been padded in 40 bytes.
As the padding is likely to be between string and integer, the first record also does not read correctly.
In this case I do not try to read your data as binary blob, and stick to reading individual fields. It is much stronger, especially if you want to change your structure too.
Comments
Post a Comment