Differences in reading from an istream vs. stringstream

Discussion in 'C++' started by david.crow@pbsnow.com, Jun 1, 2007.

  1. Guest

    Given the following input file:

    bob 1 2 3 4 5
    mary 2 3 4 5 6 7
    susan 3 4 5 6 7 8 9

    This code snippet does not read it correctly:

    class Student
    {
    public:
    ...
    private:
    std::string _Name;
    std::vector<int> _Grades;
    };
    ....
    /***** this only gets called once *****/
    std::istream& operator>>(std::istream& is, Student& s)
    {
    string name = "";
    is >> name;
    std::vector<int> grades;
    std::copy(std::istream_iterator<int>(is),
    std::istream_iterator<int>(), std::back_inserter(grades));
    /***** at this point, name and the grades vector are correct for
    the 'first' line in the input file *****/
    s.setName(name);
    s.setGrades(grades);
    return is;
    }
    ....
    std::ifstream fin;
    std::vector<Student> students;
    std::copy(std::istream_iterator<Student>(fin),
    std::istream_iterator<Student>(), std::back_inserter(students));
    /***** at this point, the students vector is empty *****/

    If I change operator>> to read from a stringstream instead, it works
    as expected.

    std::istream& operator>>(std::istream& is, Student& s)
    {
    std::string name = "";
    is >> name;

    std::string sGrades = "";
    std::getline(is, sGrades);

    std::stringstream ss(sGrades);

    std::vector<int> grades;
    std::copy(std::istream_iterator<int>(ss),
    std::istream_iterator<int>(), std::back_inserter(grades));

    s.setName(name);
    s.setGrades(grades);

    return is;
    }

    So even though the one (and only) call to operator>> read the first
    line of the file and assigned the values to the Student parameter, the
    students vector got nothing added to it. Why would changing to a
    stringstream fix this?
    , Jun 1, 2007
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > Given the following input file:
    >
    > bob 1 2 3 4 5
    > mary 2 3 4 5 6 7
    > susan 3 4 5 6 7 8 9
    >
    > This code snippet does not read it correctly:
    >
    > class Student
    > {
    > public:
    > ...
    > private:
    > std::string _Name;
    > std::vector<int> _Grades;
    > };
    > ...
    > /***** this only gets called once *****/
    > std::istream& operator>>(std::istream& is, Student& s)
    > {
    > string name = "";
    > is >> name;
    > std::vector<int> grades;
    > std::copy(std::istream_iterator<int>(is),
    > std::istream_iterator<int>(), std::back_inserter(grades));


    And what happens after the "5" in the first line is read? It will attempt
    to read "mary" and "mary" is not an integer. Puts is into an error state.

    > /***** at this point, name and the grades vector are correct for
    > the 'first' line in the input file *****/
    > s.setName(name);
    > s.setGrades(grades);
    > return is;
    > }
    > ...
    > std::ifstream fin;
    > std::vector<Student> students;
    > std::copy(std::istream_iterator<Student>(fin),
    > std::istream_iterator<Student>(), std::back_inserter(students));
    > /***** at this point, the students vector is empty *****/
    >
    > If I change operator>> to read from a stringstream instead, it works
    > as expected.
    >
    > std::istream& operator>>(std::istream& is, Student& s)
    > {
    > std::string name = "";
    > is >> name;
    >
    > std::string sGrades = "";
    > std::getline(is, sGrades);


    Yes, here you are reading one line which stops at the end of line. So for
    the first line sGrades will contain "1 2 3 4 5".

    > std::stringstream ss(sGrades);
    >
    > std::vector<int> grades;
    > std::copy(std::istream_iterator<int>(ss),
    > std::istream_iterator<int>(), std::back_inserter(grades));
    >
    > s.setName(name);
    > s.setGrades(grades);
    >
    > return is;
    > }
    >
    > So even though the one (and only) call to operator>> read the first
    > line of the file and assigned the values to the Student parameter, the
    > students vector got nothing added to it. Why would changing to a
    > stringstream fix this?


    As indicated. std::copy is attempting to read the rest of the entire file,
    names included. std::getline is attempting to read the rest of the line, no
    names included since it stops at the end of line terminator.
    Jim Langston, Jun 1, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 1, 11:21 am, "Jim Langston" <> wrote:
    >
    > As indicated. std::copy is attempting to read the rest of the entire file,
    > names included. std::getline is attempting to read the rest of the line, no
    > names included since it stops at the end of line terminator.- Hide quoted text -
    >


    Thanks for the clarification, Jim.
    , Jun 1, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    2
    Views:
    4,264
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 12, 2004
  2. morgan
    Replies:
    4
    Views:
    1,206
    Mike Wahler
    Dec 14, 2004
  3. Home_Job_opportunity
    Replies:
    0
    Views:
    495
    Home_Job_opportunity
    Jan 8, 2009
  4. Home_Job_opportunity
    Replies:
    0
    Views:
    578
    Home_Job_opportunity
    Jan 14, 2009
  5. xmllmx
    Replies:
    5
    Views:
    587
    Jorgen Grahn
    Jun 15, 2010
Loading...

Share This Page