View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12758 Members
Join this group to post and comment.
confounded
confounded • Feb 15, 2009

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.
shalini_goel14
shalini_goel14 • Feb 18, 2009
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. 😀

Share this content on your social channels -