Jul 5, 2009 at 4:23pm UTC
line 47 is the line true?.this program get endless loop.sorry my english
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
/////////////////////////////
class person
{
protected :
char name[80];
int age;
public :
void getData()
{
cout << "\nEnter name: " ; cin >> name;
cout << "\nEnter age: " ; cin >> age;
}
void showData()
{
cout << "\nName: " << name;
cout << "\nAge: " << age ;
}
};
int main(int argc, char *argv[])
{
char ch;
person pers;
fstream file;
file.open("GROUP.DAT" , ios::app | ios::out | ios::in | ios::binary);
do
{
cout << "Enter person's data: " ;
pers.getData();
file.write(reinterpret_cast <char *>(&pers),sizeof (pers) );
cout << "Enter another person(y/n)? " ;
cin >> ch;
}while (ch == 'y' );
file.seekg(0);
file.read( reinterpret_cast <char *>(&pers),sizeof (pers) );
while (!file.eof() )
{
cout << "\nPerson:" ;
pers.showData();
file.read( reinterpret_cast <char *>(&pers),sizeof (pers) );
}
cout << endl;
system("PAUSE" );
return EXIT_SUCCESS;
}
Last edited on Jul 5, 2009 at 4:24pm UTC
Jul 5, 2009 at 6:11pm UTC
I would do while (file)
instead, so in case the file breaks, you will exit.
And also:
file.write(reinterpret_cast <char *>(&pers),sizeof (pers) );
file.read( reinterpret_cast <char *>(&pers),sizeof (pers) );
Wtf is this?? You can't serialize stuff like this >_> (at least not in a readable format)
Jul 5, 2009 at 9:06pm UTC
thank u.your topic is very useful.but i must solve this program with my last code :(. i'm very tired today.life is hard but c++ more hard than life :).I don't know where's my problem