473,394 Members | 1,746 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.

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(istringstream s) // doesn't compile
but
fn(istringstream & 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 2346
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:41***************@ucalgary.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(istringstream s) // doesn't compile
but
fn(istringstream & s) // does.


How about passing it by const reference:

void fn(istringstream 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:
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:41***************@ucalgary.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(istringstream s) // doesn't compile
but
fn(istringstream & s) // does.


How about passing it by const reference:

void fn(istringstream 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(istringstream s) // doesn't compile
but
fn(istringstream & 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::istringstream &iss);

int main()
{
std::istringstream iss;

// fill 'iss'

std::istringstream 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(istringstream s) // doesn't compile
but
fn(istringstream & 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::istringstream &iss);

int main()
{
std::istringstream iss;

// fill 'iss'

std::istringstream 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(istringstream s) // doesn't compile
but
fn(istringstream & 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::istringstream &iss);

int main()
{
std::istringstream iss;

// fill 'iss'

std::istringstream 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.seekg(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
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:41***************@ucalgary.ca...
Ali Çehreli wrote:
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:41***************@ucalgary.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(istringstream s) // doesn't compile
but
fn(istringstream & s) // does.


How about passing it by const reference:

void fn(istringstream 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(istream & is)
:
is_(is),
position_(is_.tellg())
{}

~PositionSaver()
{
is_.seekg(position_);
}
};

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_iterator<string>(cin),
istream_iterator<string>(),
ostream_iterator<string>(my_stream, " "));

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
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...
8
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...
3
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...
7
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...
4
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...
4
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...
8
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
6
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
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
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
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...

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.