473,395 Members | 1,554 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,395 software developers and data experts.

Extracting from istream into stringstream

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)
{
std::stringstream ss;
source >> ss; // offending line
}

int main()
{
std::ifstream source("simples.pgm");
test(source);
}

The compiler complains about an ambiguous overload on the line where the
program tries to extract elements from the istream and into the
stringstream. Shouldn't that be a valid operation? Or, what might I be
missing?

Thank you,

--
Ney André de Mello Zunino
Jul 22 '05 #1
2 12122
Ney André de Mello Zunino wrote:
Is g++ 3.3.3 (Cygwin) correct in rejecting the following test program?

#include <istream>
#include <sstream>
#include <fstream>

void test(std::istream& source)
{
std::stringstream ss;
source >> ss; // offending line
}

int main()
{
std::ifstream source("simples.pgm");
test(source);
}

The compiler complains about an ambiguous overload on the line where the
program tries to extract elements from the istream and into the
stringstream. Shouldn't that be a valid operation? Or, what might I be
missing?


std::stream is never used like

std::stream s ...
blahblah >> s;

but always

std::stream s ...
s << blahblah;

So, your attempt is a bit misguided, I believe. You might want to do

while (source) {
char c;
source >> c;
ss << c;
}

or use 'get' and 'put' if you want to speed it up, something like

while (source)
ss.put(source.get());

Victor
Jul 22 '05 #2
Victor Bazarov wrote:
So, your attempt is a bit misguided, I believe. You might want to do


It was indeed. I realized it some time after posting the message and
even cancelled it. But you were too quick! :)

Anyway, it is almost 3:00am here and I should give my neurons a rest.

Thanks for your response, though.

--
Ney André de Mello Zunino
Jul 22 '05 #3

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

Similar topics

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...
2
by: Aleander | last post by:
Hi! I have to write the records of a vector in a file, e and then open this file to extract the record to refill the vector. My program has two class: Visita(Appointment) and Data(date). The...
3
by: Dylan | last post by:
I'm attempting to write an object's state to a std::stringstream and then to restore that state by reading from the stringstream. The writing is fine but I'm having problems reading. Here's a...
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
4
by: Johannes Zellner | last post by:
Hi, how can I extract the whole rest of an istream? (All characters which have not been consumed yet?) -- Johannes
4
by: dor | last post by:
i have an input file named input.txt where all the data looks like this: (4,10) 20 (5,3) 13 (7,19) 6 .. .. .. the numbers are random. i need to use every number in each line
4
by: nnandini8 | last post by:
Hello guys, I have a small doubt in istream. I have a file calles infile.txt infile.txt contains Hello World now I wanted to add these contents to a string. ie i need to have hello world in a...
2
by: david.crow | last post by:
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 {
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.