help- unable to write and read objects into file.

this is my code:
#include
#include
#include
#include
struct _date
{
 int dd,mm,yy;
};
struct _time
{
 int hour,min;
};
class Train
{
 private:
 int trainno;
 char from[20],to[20],station1[20];
 _date ddate,s1date,adate;
 _time dtime,s1dtime,s1atime,atime;
 int firstavail,secondavail;
 float firstamt,secondamt;
 public:
 void changeschedule();
 void display();
 int enquiry(char *f,char *t);
 int enquiry(int tno);
};
void Train::changeschedule()
{
 cout<<"\nEnter details: ";
 cout<<"\nTrain number: ";
 cin>>trainno;
 cout<<"From: ";
 gets(from);
 cout<<"To: ";
 gets(to);
 cout<<"Station 1: ";
 gets(station1);
 cout<<"Date of departure from source station: ";
 cin>>ddate.dd>>ddate.mm>>ddate.yy;
 cout<<"Date of arrival at station 1: ";
 cin>>s1date.dd>>s1date.mm>>s1date.yy;
 cout<<"\nDate of arrival at destination station: ";
 cin>>adate.dd>>adate.mm>>adate.yy;
 cout<<"Time of departure from source station: ";
 cin>>dtime.hour>>dtime.min;
 cout<<"Time of arrival at station 1: ";
 cin>>s1atime.hour>>s1atime.min;
 cout<<"Time of departure from station 1: ";
 cin>>s1dtime.hour>>s1dtime.min;
 cout<<"Time of arrival: ";
 cin>>atime.hour>>atime.min;
 cout<<"First class seats available: ";
 cin>>firstavail;
 cout<<"Second class seats available: ";
 cin>>secondavail;
 cout<<"Cost of first class ticket: ";
 cin>>firstamt;
 cout<<"Cost of second class ticket: ";
 cin>>secondamt;
}
void Train::display()
{
 cout<<"\nTrain no: "<from)&&!strcmp(t,this->to))||(!strcmp(f,this->from)
    &&!strcmp(t,this->station1))||(!strcmp(f,this->station1)
    &&!strcmp(t,this->to)))
 return 1;
 return 0;
}
int Train::enquiry(int tno)
{
 if(this->trainno==tno)
 return 1;
 return 0;
}
 
void main()
{
 clrscr();
 Train t;
 int c;
 fstream file;
 int choice;
 cout<<"\n1.New Train";
 cout<<"\n2.Enquiry from to";
 cout<<"\n3.Enquiry number";
 cout<<"\n4.Display file"; 
cin>>choice;
 if(choice==1)
 {
 file.open("tschedule",ios::app);
 do
 {
 t.changeschedule();
 file.write((char *)&t,sizeof(Train));
 cin>>c;
 }while(c);
 file.close();
 }
 else if(choice==4)
 {
 file.open("tschedule",ios::in);
 file.seekg(0);
 file.read((char *)&t,sizeof(Train));
 while(file)
 {
 t.display();
 file.read((char *)&t,sizeof(Train));
 }
 }
 else if(choice==2)
 {
  file.open("tschedule",ios::in);
  if(!file)
 return;
  int hit=0;
  char from[20],to[20];
  cout<<"From: ";
  gets(from);
  cout<<"To: ";
  gets(to);
  file.seekg(0);
  file.read((char *)&t,sizeof(Train));
  while(file)
  {
 hit=t.enquiry(from,to);
 if(hit)
  t.display();
 file.read((char *)&t,sizeof(Train));
  }
  if(!hit)
 cout<<"Not available";
  file.close();
 }
 else if(choice==3)
 {
  file.open("tschedule",ios::in);
  int hit=0;
  int tno;
  cout<<"Number: ";
  cin>>tno;
  file.seekg(0);
  file.read((char *)&t,sizeof(Train));
  while(file)
  {
 hit=t.enquiry(tno);
 if(hit)
  t.display();
 file.read((char *)&t,sizeof(Train));
  }
  if(!hit)
 cout<<"Not available";
 }
 getch();
}


I'm having a problem with files. When i write a Train object onto a file using write() function and then read it uing read() function I'm getting junk values for the data members....i checked to see if i'm making a mistake in getting input from user and display but changeschedule and display functions are working correctly. Please help.

Also, if i remove the date and time from my program it works fine so i dont know if there is a problem in using a structure within a class.

Replies

  • shalini_goel14
    shalini_goel14
    Wondering why no one is helping this guy here. Where are all the C++ users here?

    I can only say - Have you checked the file in the system, whether it is created correctly or not? One more thing Please check your enquiry function again specially check test condition there.

    If your problem is solved, please share it here also. 😀

You are reading an archived discussion.

Related Posts

Hello everyone, i need some info to start my final year project. so pls send any information you have about central air conditioning systems. technical details are appreciated. thank you....
I would like to ask help from u guys abt my project, i have just finished completing the circuit design and the idea of the project this is how my...
Hi All CEans , So you guys don't get enough time to try your hands on programming? No tensions anymore, you will be given a chance here to do simple...
Hello friends, I'm doing my final year electronics project on Voice based control of home appliances ... v r using microcontroller which takes the text input compares it with de...
HI to all this Thread reader I wanna need help for my project . It's quite intersting topic . I'm instrumentation and control enginnering final sem student,making project. The difficulties...