Trying to read and write multiple class objects from a file, fstream won't work correctly.

So I have a small program which is supposed to write and read multiple objects from a file, but for some reason it doesn't write the information when I use "fstream" to create the object from the fstream class, but it does when I use "ofstream". Also, it doesn't read the information from the file no matter if I use "fstream" or "ifstream". I watched a video where this exact code worked just fine, but it just won't work when I run it. I'm using Dev C++ 4.9.9.2, I don't know if that has anything to do with it, I also tried it with Code::Blocks, didn't work either. Here's the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <fstream>

using namespace std;

class Person
{
      protected:
                char name[20];
                int age;
      public:
             void getdata()
             {
                  cout<<"\nEnter name: ";
                  cin>> name;
                  cout<<"\nEnter age: ";
                  cin>> age;
             }
             void printdata()
             {
                  cout<<"\nName: "<<name;
                  cout<<"\nAge: "<<age;
             }
};

int main()
{
    Person pers;
    fstream file;
    char ans;

    file.open("go.txt",ios::in|ios::out|ios::ate|ios::app|ios::binary);
    do
    {
        cout<<"\nEnter Persons Data ";
        pers.getdata();
        file.write(reinterpret_cast<char*>(&pers),sizeof(pers));

        cout<<"\n\nDo you wish to enter more people:(y/n)? ";
        cin>>ans;
    }while(ans=='y');
    
    file.seekg(0);

    file.read(reinterpret_cast<char*>(&pers),sizeof(pers));

    while(!file.eof())
    {
          cout<<"\n\n\tPerson Info\n";
          pers.printdata();
          file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
          system("PAUSE");
    }
    file.close();

    cout<<"\n\n============End of Program============\n\n";

    system("PAUSE");
    return 0;
}
Works fine for me.
$ g++ --version
g++ (GCC) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4.5? you should really update. you are four minor releases behind. @op: your ide doesnt matter (although i believe dev is deprecated). its your compiler. also, are you sure you have the rights?
So I somehow managed to make the program work for me and everything's fine, thanks for you help. Now I have another problem. I changed the code so it would have a menu and it works fine, but when I select the option "Show information" it only shows the information once, when I select it again it doesn't show me anything unless I restart the program. Any ideas how I can solve this problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <fstream>

using namespace std;

class Person
{
      protected:
                char name[20];
                int age;
      public:
             void getdata()
             {
                  cout<<"\nEnter name: ";
                  cin>> name;
                  cout<<"\nEnter age: ";
                  cin>> age;
             }
             void setdata()
             {
                  cout<<"\nName: "<<name;
                  cout<<"\nAge: "<<age;
             }
};

int main()
{
    Person pers;
    ofstream file;
    ifstream outfile;
    char ans;
    int x;
    
    z:
    system("cls");
    cout<<"\n\t\tMENU\n\n";
    cout<<"\n\t 1. Enter person\n\t 2. Show information\n\t 3. Exit";
    cout<<"\n\n\n\tSelect an option: ";
    cin>>x;
    
    switch(x)
    {
          case 1:file.open("go.dat",ios::app);
                 do
                 {
                     cout<<"\nEnter Person Data ";
                     pers.getdata();
                     file.write(reinterpret_cast<char*>(&pers),sizeof(pers));

                     cout<<"\n\nDo you wish to enter more people:(y/n)? ";
                     cin>>ans;
                 }while(ans=='y');
                 file.close();
                 goto z;
                 break;
          case 2:outfile.open("go.dat",ios::ate);
                 outfile.seekg(0);

                 outfile.read(reinterpret_cast<char*>(&pers),sizeof(pers));

                 while(!outfile.eof())
                 {
                      cout<<"\n\n\tPerson Info\n";
                      pers.setdata();
                      outfile.read(reinterpret_cast<char*>(&pers),sizeof(pers));
                 }
                 outfile.close();
                 cout<<"\n\n\tDo you want to cotinue: (y/n)?";
                  cin>>ans;
                  if(ans=='y')
                     goto z;
                  else
                     exit(1);
                 break;
          case 3:exit(1);
                 break;
          default:cout<<"\n\n\t\twrong option";
                  cout<<"\n\t\tDo you want to cotinue: (y/n)?";
                  cin>>ans;
                  if(ans=='y')
                     goto z;
                  else
                     exit(1);
    }
    return 0;
}
Topic archived. No new replies allowed.