data file handling

i have made a class with some private members and a public function suppose f1 to input the private members n manipulate them to be called in the main func. but when i use an fstream obj in func f1 to store the private data of the class in a file, i get an error that the fstream obj was not defined.
my question is how to store the private members of a class in a file....
thanks in adv, pls help me out
Last edited on
Typically you will overload the << and >> operators to serialize your object to and from a file. How exactly that object is represented is up to you.

For example, given a list of students and their grades, I may store each student on a single line as
last-name first-name grade1 grade2 grade3 ...


So, let's do it:
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
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct Student
  {
  string surname, givenname;
  vector <int> grades;

  string name() const { return givenname +" " +surname; }

  friend istream& operator >> ( istream& ins,  Student& student );
  friend ostream& operator << ( ostream& outs, Student& student );
  };

istream& operator >> ( istream& ins, Student& student )
  {
  int grade;
  string s;
  getline( ins, s );
  stringstream ss( s );
  ss >> student.surname;
  ss >> student.givenname;
  while (ss >> grade) student.grades.push_back( grade );
  if (!ss.good() && !ss.eof()) ins.setstate( ss.rdstate() );
  return ins;
  }

ostream& operator << ( ostream& outs, Student& student )
  {
  outs << student.surname << ' ' << student.givenname;
  for (vector <int> ::iterator grade = student.grades.begin(); grade != student.grades.end(); grade++)
    outs << ' ' << *grade;
  outs << endl;
  return outs;
  }

Now you can read and write students with ease.
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
int main()
  {
  Student          student;
  vector <Student> students;
  string           filename, s;
  unsigned         index;

  // Open the student grades file
  cout << "Please enter the students filename> ";
  getline( cin, filename );

  ifstream inf( filename.c_str() );
  if (!inf)
    {
    cout << "I could not open \"" << filename << "\"\n";
    return 1;
    }

  // Read all students from file
  while (inf >> student)
    students.push_back( student );

  inf.close();

  // Display student names
  for (unsigned n = 0; n < students.size(); n++)
    cout << n << ": " << students[ n ].givenname << ' ' << students[ n ].surname << '\n';

  // Select a student
  cout << "\nWhich student's grades would you like to see? ";
  getline( cin, s );
  if (!(stringstream( s ) >> index))
    {
    for (index = 0; index < students.size(); index++)
      if (s == students[ index ].name()) break;
    }
  if (index >= students.size())
    {
    cout << "No such student exists.\n";
    return 1;
    }

  // Display that student
  cout << students[ index ];

  cout << "\nDone.\n";

  return 0;
  }

Here's a little test file to play with.
1
2
3
Jimenez Jorge 100 100 90
Brown Anita 78 100 100
Murphy Duncan 100 90 100


Hope this helps.
I tryed to run this code and i found out that when you want to see the grades for Brown Anita the grades are "100 100 90 78 100 100" and for Murphy Duncan "100 100 90 78 100 100 100 90 100".
I added a cout << student.grades.size(); at the beginning of the ">>" function and the size is "0 3 6"(each one for every time it enters the function). That means that it still has the values from the pevious entrys.
If i put a student.grades.clear(); then it works fine...

Isn't it supposed, the vector "grades", to be empty because it is a new vector object?

Last edited on
But vector isn't a new object. Thats the trick in this code.
Line 3: Student student;
Local object of type student. The values are never cleared, it's just re-populated manually. But never clears the grades vector from the previous entry.
Oh, i got what you mean. Because we always use the same student object to hande the values the vector in it is never new.

We want to have each student in its own struct with its grades. Shouldn't we clear the struct vector before we push the grades of each student?
Last edited on
Topic archived. No new replies allowed.