c++ - read objects from file -
i have code:
#include <fstream> #include <iostream> using namespace std; class student { public: char fullname[40]; char completeaddress[120]; char gender; double age; bool livesinasingleparenthome; }; int main() { student one; strcpy(one.fullname, "ernestine waller"); strcpy(one.completeaddress, "824 larson drv, silver spring, md 20910"); one.gender = 'f'; one.age = 16.50; one.livesinasingleparenthome = true; ofstream ofs("fifthgrade.ros", ios::binary); ofs.write((char *)&one, sizeof(one)); student three; strcpy(three.fullname, "three waller"); strcpy(three.completeaddress, "three 824 larson drv, silver spring, md 20910"); three.gender = 'm'; three.age = 17; three.livesinasingleparenthome = true; //ofstream ofs("fifthgrade.ros", ios::binary); ofs.write((char *)&three, sizeof(three));*/ student two; ifstream ifs("fifthgrade.ros", ios::binary); while(!(ifs.eof())){ ifs.read((char *)&two, sizeof(two)); cout << "student information\n"; cout << "student name: " << two.fullname << endl; cout << "address: " << two.completeaddress << endl; if( two.gender == 'f' || two.gender == 'f' ) cout << "gender: female" << endl; else if( two.gender == 'm' || two.gender == 'm' ) cout << "gender: male" << endl; else cout << "gender: unknown" << endl; cout << "age: " << two.age << endl; if( two.livesinasingleparenthome == true ) cout << "lives in single parent home" << endl; else cout << "doesn't live in single parent home" << endl; cout << "\n"; } return 0; }
when read file, last object prints twice. should do?
try
while(ifs.read((char *)&two, sizeof(two)))
instead of
while(!(ifs.eof()))
also try formatting code :)
#include <fstream> #include <iostream> using namespace std; class student { public: char fullname[40]; char completeaddress[120]; char gender; double age; bool livesinasingleparenthome; }; int main() { /*student one; strcpy(one.fullname, "ernestine waller"); strcpy(one.completeaddress, "824 larson drv, silver spring, md 20910"); one.gender = 'f'; one.age = 16.50; one.livesinasingleparenthome = true; ofstream ofs("fifthgrade.ros", ios::binary); ofs.write((char *)&one, sizeof(one)); student three; strcpy(three.fullname, "three waller"); strcpy(three.completeaddress, "three 824 larson drv, silver spring, md 20910"); three.gender = 'm'; three.age = 17; three.livesinasingleparenthome = true; //ofstream ofs("fifthgrade.ros", ios::binary); ofs.write((char *)&three, sizeof(three));*/ student two; ifstream ifs("fifthgrade.ros", ios::binary); while(ifs.read((char *)&two, sizeof(two))) { cout << "student information\n"; cout << "student name: " << two.fullname << endl; cout << "address: " << two.completeaddress << endl; if( two.gender == 'f' || two.gender == 'f' ) cout << "gender: female" << endl; else if( two.gender == 'm' || two.gender == 'm' ) cout << "gender: male" << endl; else cout << "gender: unknown" << endl; cout << "age: " << two.age << endl; if( two.livesinasingleparenthome == true ) cout << "lives in single parent home" << endl; else cout << "doesn't live in single parent home" << endl; cout << "\n"; } return 0; }
Comments
Post a Comment