Reading from File question... [modified]
-
Here is the most flexible way:
#include <vector> #include <algorothm> #include <iterator> #include <fstream> class Student { Student() : _StudentID(0) {} ~Student() {} void setStudentID(long id) {_StudentID = id; } long getStudentID() const { return _StudentID; } std::vector<long>::iterator grades_begin() { return _Grades.begin(); } std::vector<long>::iterator grades_end() { return _Grades.end(); } void addGrade(long grade) {_Grades.push_back(grade); } void addGrades(const std::vector<long>& grades) { _Grades.insert(back_inserter(_Grades), grades.begin(), grades.end()); } unsigned long grade_count() const { return _Grades.size(); } private: long _StudentID; std::vector<long> _Grades; }; ostream& operator<<(ostream& os, const Student& s) { os << s.getStudentID() << " "; copy(s.begin(), s.end(), ostream_iterator<long>(os, " ")); os << endl; return os; } istream& operator>>(istream& is, Student& s) { long id; std::vector<long> grades; is >> id; copy(istream_iterator<long>(is), istream_iterator<long>(), back_inserter(grades)); s.setStudentID(id); s.addGrades(grades); return is; } int main() { ifstream fin; fin.open("mydata.txt"); std::vector<Student> students; copy(istream_iterator<Student>(fin), istream_iterator<Student>(), back_inserter(students)); fin.close(); }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Zac, I was going back over this code and was confused on a few things. In
main()
, the call tocopy()
is made once for the entire file. This, in turn, is supposed to calloperator>>
once for each line of the file. Is that correct? Using the debugger, I seeoperator>>
getting called only for the first line of the file. I'm not seeing thestudents
vector grow at all. What am I missing? Thanks, DC
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Zac, I was going back over this code and was confused on a few things. In
main()
, the call tocopy()
is made once for the entire file. This, in turn, is supposed to calloperator>>
once for each line of the file. Is that correct? Using the debugger, I seeoperator>>
getting called only for the first line of the file. I'm not seeing thestudents
vector grow at all. What am I missing? Thanks, DC
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Looking back at it, is a slight bug in my example code. Basically, since the id's and grades are both longs, there is no way to tell them apart. It would probably be helpful to modify the output slightly by putting a grade count in between the student id and the first grade so that you can modify the copy call (for obtaining the grades) to just grab that many items (instead of grabbing everything that is left in the file). Another (possibly simpler alternative) would be to grab the student id, and then use getline to obtain the rest of the line, place that string into a stringstream and then call copy on it (this solution has the added benefit of not having to change the input/output file format).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Looking back at it, is a slight bug in my example code. Basically, since the id's and grades are both longs, there is no way to tell them apart. It would probably be helpful to modify the output slightly by putting a grade count in between the student id and the first grade so that you can modify the copy call (for obtaining the grades) to just grab that many items (instead of grabbing everything that is left in the file). Another (possibly simpler alternative) would be to grab the student id, and then use getline to obtain the rest of the line, place that string into a stringstream and then call copy on it (this solution has the added benefit of not having to change the input/output file format).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
My bad, Zac. I meant to comment on this thread.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
My bad, Zac. I meant to comment on this thread.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
No problem. I figured as much ;) It wouldn't take much to get that to be working code, but originally it was just meant as a nudge in the right direction.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac