Help Please! My program crashes for no reason

shkhanal's Avatar, Join Date: Aug 2010
Newbie Member

I intend to store objects in file and retrieve them. But the following program crashes when I compile and run on second time. for first time it runs. I guess there is some problem with append mode because if mode is selected output it runs without problem.

I'm using Dev-c++ to write and compile this program in Windows 7 OS.

Can you help me to identify the problem. I'm struck here when I need to write a program for payroll system of an organization (exam question).


using namespace std;

ofstream fout;
fstream fin;

class game{
      string name;
      int year;
      game(){name="Olympic"; year=2004;}
      game(string n, int y) {name=n; year=y;}
      string getName(){ return name;}
      int getYear(){ return year;}

int main(){
   game g("World Cup",2010);
   game g10;"test.dat",ios::binary|ios::app);
   fout.write((char*) &g, sizeof(g));

      { cout<<"---End of File Reached ---"<<endl;
  *) &g10, sizeof(g10));
            cout<<"\nName of Game: "<<   g10.getName();
            cout<<"\nYear of Game: "<<g10.getYear();
Ancient Dragon's Avatar, Join Date: Jul 2010
Go4Expert Member
The read loop is wrong. Its not necessary to call eof()
while(*) &g10, sizeof(g10))
   // blabla
shabbir like this
puneet306's Avatar, Join Date: Jul 2010
Newbie Member
i think there is no prob with ur append mode but i think, when u are opening the file for writing but u are not using "out" mode. if this does not solve ur prob, then u can change the extension to ".txt" rather than using .dat mode there can be prob with ur one of "data types". also try to DEBUG the program using breakpoints