-4

When I press "2" to read the file, the data in the file can't be read although my syntax is correct. Why can't my program read the data? What is the difference between sequential and random access file and why is a random access file preferred?

void EnterRecord();
void ShowRecord();
using namespace std;
class student
{
    int rollno;
    string name;
    int marks;
    public:
        void SetData();
        void DisplayData();
        void MainMenu();
};
   void student:: MainMenu()
   {
      cout<<"                             "<<endl;
      cout<<"press 1 to enter record"<<endl;
      cout<<"press 2 to show record"<<endl;
   }
   void student::SetData()
   {
      cout<<"enter roll no of student"<<endl;
      cin>>rollno;
      cout<<"enter name of student"<<endl;
      cin>>name;
      cout<<"enter marks of student"<<endl;
      cin>>marks;
   }
   void student::DisplayData()
   {
      cout<<rollno<<setw(10)<<setw(10)<<marks<<endl;
   }

     int main()
     {
            student s1;
        int choice;
        repeat:
        s1.MainMenu();
        cout<<"enter ur choice ::"<<endl;
        cin>>choice;

        switch(choice)
        {
            case 1:
                EnterRecord();
                break;
                case 2:
                    ShowRecord();
                    break;
         }
           return 0;
     }

      void  EnterRecord()
   {
      ofstream myfile;
      myfile.open("student3.txt",ios::app);
      int choice=1;
      while(choice==1)
      {
        student s1;
          s1.SetData();

          myfile.write((char*)&s1,sizeof(student));
          cout<<"press 1 to enter record again"<<endl;
          cin>>choice;
          if(choice!=1)
          {
            system("cls");
           s1.MainMenu();
      }
         }
           myfile.close();
   }
     void ShowRecord()
     {
        student s2;
         ifstream myfile;
         myfile.open("student3.txt");
         myfile.seekg(0);
         while(myfile.eof())
         {
            myfile.read((char*)&s2,sizeof(student));
          }
          s2.DisplayData();
     }
08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
jawad
  • 11
  • 4
  • 4
    Your `case` expressions are incorrect. They should be `'1'` and `'2'` instead of simply `1` and `2`. You should also start checking for errors returned by the functions you call. Also see [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Captain Obvlious Jul 17 '15 at 14:48
  • 5
    Please don't casually add completely different questions to the end of your sentence... – Lightness Races in Orbit Jul 17 '15 at 14:51
  • Questions seeking debugging help (**"why isn't this code working?"**) must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a **clear** problem statement are not useful to other readers – Prerak Sola Jul 17 '15 at 14:56

2 Answers2

1

The big problem is that you are treating an instance of student as if it were a single block of plain old data (POD) when it's not. It contains an instance of std::string which is has internal data you are not aware of. The first and most obvious is that it contains a pointer to string data. When you write it out you are saving the pointer value instead of the actual string. When you load it back in it reads the old pointer value which will only be valid if you're a really lucky leprechaun.

Your functions EnterRecord and ShowRecord will need to be updated so that they serialize each member of student instead of writing it out as a single blob of plain old data. Something like the following should get you started.

Saving data:

myfile << st.rollno;
myfile << st.name;
myfile << st.marks;

Loading data

myfile >> st.rollno;
myfile >> st.name;
myfile >> st.marks;

Since you didn't provide any information on how the file should be laid out it's up to you to decide exactly how you do it. Traditionally you would overload operator>> and operator<< but since this looks like home work that might be a bit much.

There are several other issues with your code as well...

  1. Your case expressions are incorrect. You are using values 1 and 2 which are different than the ASCII values for the 1 and 2 keys. You should use '1' and '2' instead of simply 1 and 2.

  2. You should also start checking for errors returned by the functions you call such as open.

  3. Checking for eof in your while loop is definitely not going to work. See the Q&A @ Why is iostream::eof inside a loop condition considered wrong?

Community
  • 1
  • 1
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
0

Should be:

switch(choice)
{
    case '1':
        EnterRecord();
        break;
    case '2':
        ShowRecord();
        break;
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • That's not the only problem. The use of `eof` in the `while` loop won't work correctly and `myfile.read((char*)&s2,sizeof(student));` is **so very totally absolutely wrong** – Captain Obvlious Jul 17 '15 at 15:04
  • switch statements is working the problem is with reading the data from the file. – jawad Jul 17 '15 at 15:04
  • @jawad No, it's not. the value of literal `'1'` is _not_ the same as `1` (which is used in the `case` expression). So either it's not working or you didn't post your real code. – Captain Obvlious Jul 17 '15 at 15:05
  • @CaptainObvlious can you suggest me the syntax – jawad Jul 17 '15 at 15:06
  • @CaptainObvlious what is the problem with this myfile.read((char*)&s2,sizeof(student)); can you explain – jawad Jul 17 '15 at 15:11