Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reading from File question... [modified]

Reading from File question... [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++iosdata-structureshelp
24 Posts 6 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Zac Howland

    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

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #21

    Zac, I was going back over this code and was confused on a few things. In main(), the call to copy() is made once for the entire file. This, in turn, is supposed to call operator>> once for each line of the file. Is that correct? Using the debugger, I see operator>> getting called only for the first line of the file. I'm not seeing the students 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

    Z 1 Reply Last reply
    0
    • D David Crow

      Zac, I was going back over this code and was confused on a few things. In main(), the call to copy() is made once for the entire file. This, in turn, is supposed to call operator>> once for each line of the file. Is that correct? Using the debugger, I see operator>> getting called only for the first line of the file. I'm not seeing the students 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

      Z Offline
      Z Offline
      Zac Howland
      wrote on last edited by
      #22

      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

      D 1 Reply Last reply
      0
      • Z Zac Howland

        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

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #23

        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

        Z 1 Reply Last reply
        0
        • D David Crow

          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

          Z Offline
          Z Offline
          Zac Howland
          wrote on last edited by
          #24

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups