storing data, best possible way?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 574 Views - Last Post: 23 March 2013 - 08:36 AM Rate Topic: -----

#16 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: storing data, best possible way?

Posted 23 March 2013 - 08:36 AM

Like Jim said, you may want to tackle an easier file first?

Say, something like this ...
id dob name
1 20000101 Jane Doe
5 19820611 Joe
4 19991202 Marlene Anne Robinson



Then you could have a struct like ...
struct Student
{
   int id;
   int dob;
   string name;
} ;



Then you could hold all your data in a C++ vector

// a common C++ idiom is to code something like this ...
const string fName = "myData.txt";
ifstream fin( fName.c_str() );
if( fin )
{ 
   vector < Student > v;
   string line;
   while( getline( fin, line ) )
   {
      istringstream iss( line );
      Student s;
      iss >> s.id >> s.dob; // skips leading ws
      getline( iss, line); // get rest of iss line into line, preserve any leading ws
      ltrim( line ); // trim off any leading ws
      s.name = line; // add the name trimmed of leading ws
      v.push_back( s ); // add this student struct to vector 
   }
   fin.close();
}
else cout << "There was a problem opening file " << fName << endl;


This post has been edited by David W: 23 March 2013 - 10:27 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2