473,545 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about istringstream

I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.
ie when the function returns I want the istringstream to be
unmodified...

However when I try to pass it

fn(istringstrea m s) // doesn't compile
but
fn(istringstrea m & s) // does.

What should I do to?

I tried to make a copy:

istringstream hold = s
but that doesn't compile either...

help! :)
Jul 22 '05 #1
6 2360
"JustSomeGu y" <No***@ucalgary .ca> wrote in message
news:41******** *******@ucalgar y.ca...
I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.
istringstream (and possibly any stream) cannot be copied.
ie when the function returns I want the istringstream to be
unmodified...

However when I try to pass it

fn(istringstrea m s) // doesn't compile
but
fn(istringstrea m & s) // does.


How about passing it by const reference:

void fn(istringstrea m const & s);

But then, you can't do much with a const input stream anyway. What are
trying to achieve?

Ali

Jul 22 '05 #2
Ali Çehreli wrote:
"JustSomeGu y" <No***@ucalgary .ca> wrote in message
news:41******** *******@ucalgar y.ca...
I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.
istringstream (and possibly any stream) cannot be copied.
ie when the function returns I want the istringstream to be
unmodified...

However when I try to pass it

fn(istringstrea m s) // doesn't compile
but
fn(istringstrea m & s) // does.


How about passing it by const reference:

void fn(istringstrea m const & s);

But then, you can't do much with a const input stream anyway. What are
trying to achieve?


Just tying to extract data from the stream and leave it the same state.

Ali


Jul 22 '05 #3
> I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.
streams can't be copied.
ie when the function returns I want the istringstream to be
unmodified...
make it a const reference.
However when I try to pass it

fn(istringstrea m s) // doesn't compile
but
fn(istringstrea m & s) // does.

What should I do to?


fn(const istringstream &s)

If you really need to work on a copy, do it manually (but the parameter
will always have to be a const reference because you can't pass streams
by value).

void fn(const std::istringstr eam &iss);

int main()
{
std::istringstr eam iss;

// fill 'iss'

std::istringstr eam copy_of_iss;
copy_of_iss.str (iss.str());

fn(copy_of_iss) ;
}

So fn() works on a reference to a copy.

Jonathan
Jul 22 '05 #4
Jonathan Mcdougall wrote:
I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.
streams can't be copied.
ie when the function returns I want the istringstream to be
unmodified...


make it a const reference.
However when I try to pass it

fn(istringstrea m s) // doesn't compile
but
fn(istringstrea m & s) // does.

What should I do to?


fn(const istringstream &s)

If you really need to work on a copy, do it manually (but the parameter
will always have to be a const reference because you can't pass streams
by value).

void fn(const std::istringstr eam &iss);

int main()
{
std::istringstr eam iss;

// fill 'iss'

std::istringstr eam copy_of_iss;
copy_of_iss.str (iss.str());

fn(copy_of_iss) ;
}

So fn() works on a reference to a copy.


This sounds like a good idea.. but when you make a copy of the iss stream
does the current 'stream buffer point' get preserved..
ie if you've read (red) 3 characters from the input stream will you get
the fourth character on the next read?
I guess the bottom line is that the operator= hasn't been defined for this
stream class...

Jonathan


Jul 22 '05 #5
JustSomeGuy wrote:
Jonathan Mcdougall wrote:

I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.
streams can't be copied.

ie when the function returns I want the istringstream to be
unmodified.. .


make it a const reference.

However when I try to pass it

fn(istringst ream s) // doesn't compile
but
fn(istringst ream & s) // does.

What should I do to?


fn(const istringstream &s)

If you really need to work on a copy, do it manually (but the parameter
will always have to be a const reference because you can't pass streams
by value).

void fn(const std::istringstr eam &iss);

int main()
{
std::istringstr eam iss;

// fill 'iss'

std::istringstr eam copy_of_iss;
copy_of_iss.str (iss.str());

fn(copy_of_iss) ;
}

So fn() works on a reference to a copy.

This sounds like a good idea.. but when you make a copy of the iss stream


I don't, I defined a new object using the same string as its buffer.
does the current 'stream buffer point' get preserved..
Of course not, nor its state or whatever data it has, nothing got
copied. I just made a new istringstream and set its buffer to the same
string as the first one.

Particularily, to set the buffer's read position, just do something like

copy_of_iss.see kg(iss.tellg()) ;
I guess the bottom line is that the operator= hasn't been defined for this
stream class...


The bottom line is: streams are not copyable by design.
Jonathan
Jul 22 '05 #6
"JustSomeGu y" <No***@ucalgary .ca> wrote in message
news:41******** *******@ucalgar y.ca...
Ali Çehreli wrote:
"JustSomeGu y" <No***@ucalgary .ca> wrote in message
news:41******** *******@ucalgar y.ca...
I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.


istringstream (and possibly any stream) cannot be copied.
ie when the function returns I want the istringstream to be
unmodified...

However when I try to pass it

fn(istringstrea m s) // doesn't compile
but
fn(istringstrea m & s) // does.


How about passing it by const reference:

void fn(istringstrea m const & s);

But then, you can't do much with a const input stream anyway. What are
trying to achieve?


Just tying to extract data from the stream and leave it the same state.


Unfortunately this is not guaranteed to work for input streams. Think about
the standard input: Characters taken out of it are gone forever unless we
save them.

The best you can do is to save all of the characters into a stream. Then you
can use seekg to set the read position before returning from your function.

The following program worked for me. Send two words to the standard input of
the program to see that consecutive calls to foo maintain the read position
of the stream.

#include <sstream>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

class PositionSaver
{
istream & is_;
ios::pos_type position_;

public:

explicit PositionSaver(i stream & is)
:
is_(is),
position_(is_.t ellg())
{}

~PositionSaver( )
{
is_.seekg(posit ion_);
}
};

void foo(istream & input)
{
PositionSaver const position_saver( input);

string first_word;
input >> first_word;
cout << "first word read in foo : " << first_word << '\n';
}

int main()
{
stringstream my_stream;
copy(istream_it erator<string>( cin),
istream_iterato r<string>(),
ostream_iterato r<string>(my_st ream, " "));

string first_word;
my_stream >> first_word;
cout << "first word read in main: "
<< first_word << '\n';

foo(my_stream);
foo(my_stream);
}

Ali

Jul 22 '05 #7

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

Similar topics

1
3419
by: Samuele Armondi | last post by:
Hi everyone, Since istringstream objects are not assignable, I'm using the following code to allocate some dynamically. My question is: Is this the correct way of doing it? Am I deleting all the allocated memory correctly? Or am I missing something glaringly simple? Thanks in advance, S. Armondi std::istringstream** ArgStream;
8
5355
by: Agent Mulder | last post by:
I try to remove the spaces from a string using an old trick that involves an istringstream object. I expect the while-condition while(istringstream>>string) to evaluate to false once the istringstream is exhausted, but that is not the case. What am I missing? #include<iostream> #include<sstream> #include<string>
3
4719
by: bml | last post by:
Could you help and answer my questions of istringstream? Thanks a lot! 1. Reuse an "istringstream" istringstream ist; ist.str("This is FIRST test string"); ist.str("This is SECOND test string"); cout << ist.str() << endl;
7
748
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream encounters the two byte shorts, it either thinks it has reached the null terminator? or eof and consequently stops reading values back in. It doesn't...
4
2704
by: Brandon | last post by:
I'm using an istringstream to convert some integers stored in string form to integers. However, each integer is stored in a differnt string. So, I used istringstream's str(string) method for the first string and then used the >> operator to extract the integer. It worked just fine. So, on to the second string. I used str(string) again...
4
2315
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output data. The data comes in string lines like "OREBlegQ 14854 731.818" which need to be split into a string, int and double when stored in the class....
8
2079
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
6
2984
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
11
2885
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
0
7432
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
7689
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7456
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
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
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
5359
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
5076
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
3490
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
1044
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.