473,569 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::istre am& source)
{
std::stringstre am 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 12197
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::istre am& source)
{
std::stringstre am 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.g et());

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
7723
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 = std::stringstream(); // fails Error is:
3
5691
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 binary file through a unix pipe to the program (cat file | prog) everything works fine. However, when I feed data from a program, one that connects...
2
1866
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 second one is used in the first one such as a field. I wrote the scrivi() function to write the record in a file called "archivio.txt" and it works...
3
3841
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 little prog that demonstrates the problem: #include <iostream> #include <sstream> int main() {
13
3720
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 these methods EXTRACT the data at one point or another. The other problem is there appears to be NO WAY to get at the actual buffer pointer (char*) of...
4
3533
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
2557
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
3926
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 string Note there is a space between Hello and WOrld Any help will be greatly appreciated Thank You
2
7835
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
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.