473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Differences in reading from an istream vs. stringstream

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<intgrades;
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<Studentstudents;
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<intgrades;
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 '07 #1
2 7811
<da********@pbsnow.comwrote in message
news:11********************@m36g2000hse.googlegrou ps.com...
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<intgrades;
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<Studentstudents;
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<intgrades;
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.
Jun 1 '07 #2
On Jun 1, 11:21 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>
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 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marc Schellens | last post by:
The following routine ends up always the fail bit set. I can extract from the same stream e.g. floats without a problem (so it cannot be the stream) Any obivious thing I missed? Thanks, marc ...
24
by: Hendrik Schober | last post by:
Hi, I have a 'std::istream' and need to read its whole contents into a string. How can I do this? TIA; Schobi
2
by: Ney André de Mello Zunino | last post by:
Hello. Is g++ 3.3.3 (Cygwin) correct in rejecting the following test program? #include <istream> #include <sstream> #include <fstream> void test(std::istream& source) {
4
by: SerGioGio | last post by:
Hello, I have problems understanding why the following example does not compile in VC7.1: std::stringstream test; const std::istream& test2 = test; //OK const std::istream& test3 =...
3
by: lpe540 | last post by:
Hi, I'm having trouble using istream to read in a file in its entirety on UNIX. I've written a dummy program that essencially reads in a file from stdin and writes it out to a file. When I cat a...
3
by: Ivan Liu | last post by:
I would like know how I can skip a line while reading a set of input data (from a text file) if the first character of the line is "#". My original code reads: ifstream Infile("data.dat"); ...
4
by: isaac2004 | last post by:
hello, what i am trying to do is set up a loop that grabs multiple variables from an input file. my question is, is there a built in function in the istream class that can grab multiple values on...
1
jwwicks
by: jwwicks | last post by:
Hello All, This is a student assignment. So I don't want the complete answer just a hint or maybe a bumb on the head cause I'm doing it the wrong way. Assume I haven't done anything braindead like...
9
by: Bint | last post by:
i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry" etc i can use sscanf(string,"success=%d", &d) to get the success value. but after that i just want to read name and u pairs...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.