CrazyEngineers
  • help- unable to write and read objects into file.

    confounded

    Member

    Updated: Oct 26, 2024
    Views: 1.2K
    this is my code:
    #include<fstream.h>
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    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: "<<trainno
         <<"\nFrom: "<<from
         <<"\nTo: "<<to
         <<"\nStation 1: "<<station1
         <<"\nDate of departure from source station: "
         <<ddate.dd<<"|"<<ddate.mm<<"|"<<ddate.yy
         <<"\nDate of arrival at station 1: "
         <<s1date.dd<<"|"<<s1date.mm<<"|"<<s1date.yy
         <<"\nDate of arrival at destination station: "
         <<adate.dd<<"|"<<adate.mm<<"|"<<adate.yy
         <<"\nTime of departure from source station: "
         <<dtime.hour<<"."<<dtime.min
         <<"\nTime of arrival at station 1: "
         <<s1atime.hour<<s1atime.min
         <<"\nTime of departure from station 1: "
         <<s1dtime.hour<<s1dtime.min
         <<"\nTime of arrival at destination station: "
         <<atime.hour<<"."<<atime.min
         <<"\nFirst class seats available: "<<firstavail
         <<"\nSecond class seats available: "<<secondavail
         <<"\nFirst class seat amount: "<<firstamt
         <<"\nSecond class seat amt: "<<secondamt;
    }
    int Train::enquiry(char *f,char *t)
    {
     if((!strcmp(f,this->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.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • shalini_goel14

    MemberFeb 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. 😀
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register