reading and outputting data from a binary file...
-
I am having difficulty reading from a binary file. For some reason, the program is not reading from it properly. My code is short and so I will show all of it
#include #include struct Info { //unsigned short ID; int ID; float GPA, FinAid; char state[3]; char gender[2]; char Fname[15]; char Lname[15]; char status[15]; }; void main(void) { fstream people; Info person; char again; people.open("C:\\Student.bin", ios::in | ios::binary); if(!people) { cout << "Error Opening File! Program Aborting!!\n\n"; return; } people.read((char *)&person, sizeof(person)); while (!people.eof()) { cout << "ID: "; cout << person.ID << endl; cout << "GPA: "; cout << person.GPA << endl; cout << "Financial Aid: "; cout << person.FinAid << endl; cout <<"State: "; cout << person.state << endl; cout << "Gender: "; cout << person.gender << endl; cout << "First Name: "; cout << person.Fname << endl; cout << "Last Name: "; cout << person.Lname << endl; cout << "Status: "; cout << person.status << endl; cin.get(again); people.read((char *)&person, sizeof(person)); } people.close(); }
When I debug it, it will not gather the correct data and also show weird characters. I get the feeling that it is trying to read the file in text file mode still. Not sure why as I've checked examples through the internet and I have pretty much the same code. -
I am having difficulty reading from a binary file. For some reason, the program is not reading from it properly. My code is short and so I will show all of it
#include #include struct Info { //unsigned short ID; int ID; float GPA, FinAid; char state[3]; char gender[2]; char Fname[15]; char Lname[15]; char status[15]; }; void main(void) { fstream people; Info person; char again; people.open("C:\\Student.bin", ios::in | ios::binary); if(!people) { cout << "Error Opening File! Program Aborting!!\n\n"; return; } people.read((char *)&person, sizeof(person)); while (!people.eof()) { cout << "ID: "; cout << person.ID << endl; cout << "GPA: "; cout << person.GPA << endl; cout << "Financial Aid: "; cout << person.FinAid << endl; cout <<"State: "; cout << person.state << endl; cout << "Gender: "; cout << person.gender << endl; cout << "First Name: "; cout << person.Fname << endl; cout << "Last Name: "; cout << person.Lname << endl; cout << "Status: "; cout << person.status << endl; cin.get(again); people.read((char *)&person, sizeof(person)); } people.close(); }
When I debug it, it will not gather the correct data and also show weird characters. I get the feeling that it is trying to read the file in text file mode still. Not sure why as I've checked examples through the internet and I have pretty much the same code.Where's the code that writes to the file? Regards Senthil My Blog
-
Where's the code that writes to the file? Regards Senthil My Blog
-
Sorry if I wasn't clear. The program does not write to the file but rather reads in the information then outputs it on the screen for the user to see. It does not write to the binary file.
You can read only information that was previously written. Your program (or some other program) must have written the exact same structure instances to a file. Only then will your code work. Regards Senthil My Blog
-
I am having difficulty reading from a binary file. For some reason, the program is not reading from it properly. My code is short and so I will show all of it
#include #include struct Info { //unsigned short ID; int ID; float GPA, FinAid; char state[3]; char gender[2]; char Fname[15]; char Lname[15]; char status[15]; }; void main(void) { fstream people; Info person; char again; people.open("C:\\Student.bin", ios::in | ios::binary); if(!people) { cout << "Error Opening File! Program Aborting!!\n\n"; return; } people.read((char *)&person, sizeof(person)); while (!people.eof()) { cout << "ID: "; cout << person.ID << endl; cout << "GPA: "; cout << person.GPA << endl; cout << "Financial Aid: "; cout << person.FinAid << endl; cout <<"State: "; cout << person.state << endl; cout << "Gender: "; cout << person.gender << endl; cout << "First Name: "; cout << person.Fname << endl; cout << "Last Name: "; cout << person.Lname << endl; cout << "Status: "; cout << person.status << endl; cin.get(again); people.read((char *)&person, sizeof(person)); } people.close(); }
When I debug it, it will not gather the correct data and also show weird characters. I get the feeling that it is trying to read the file in text file mode still. Not sure why as I've checked examples through the internet and I have pretty much the same code.The problem is probably due to the member structure alignement: the data in the file have probably been saved with another program which specified a member struct alignement of 1. Member struct alignement means on which byte boundaries are stored all the fields of your structure in memory. Thus, specifying a member struct of 8 (the default), every fields of the structure start on a 8 byte boundary. When you try to read directly data from a file and 'copy' it in memory, if the structure was not saved with the same member struct alignement, this will results in misalignement of your fields (and so, you won't be able to read anything). To avoid the problem, try to set the alignement of your structure to 1:
#pragma pack(1) struct Info { //unsigned short ID; int ID; float GPA, FinAid; char state[3]; char gender[2]; char Fname[15]; char Lname[15]; char status[15]; }; #pragma pack // reset to default