-
February 10th, 2009, 04:17 PM
#1
Clearing I/O stream
Ok, I've been searching google for about 30 minutes and can't find the answer to this question. I suspect it's trivial, but I don't know.
The problem is I want to build a string out of a combination of characters and integers.
I have:
Code:
ostringstream sout;
basic_string<char> exotic_name;
vector< basic_string<char> > file_names;
int exotic_num;
int i, j;
for( i = 1; i <= exotic_num; i++ )
{
for( j = 1; j <= exotic_num; j++ )
{
sout.clear();
sout << exotic_name << i << '_' << "bar" << exotic_name << j;
file_names.push_back( sout.str() );
}
}
Ok, so the problem is this: when I run the code, the sout.clear() line doesn't seem to do anything. That is, the last entry in the vector file_names contains ALL of the data in the loop. I want to clear the output from sout after each loop iteration, but have no idea about how to do this.
Help is appreciated greatly!
-
February 10th, 2009, 04:19 PM
#2
Re: Clearing I/O stream
PS---I can post the whole code if you need to see what else is going on, but I think this is the relevant part.
-
February 10th, 2009, 05:01 PM
#3
Re: Clearing I/O stream
Take a look at this:
http://www.cplusplus.com/reference/i...ios/clear.html
The clear function simply resets a bunch of flags associated with the stream state. I don't see how it would do what you expect.
Try this:
sout.str("") to reset it to a null string.
-
February 10th, 2009, 05:10 PM
#4
-
February 10th, 2009, 05:10 PM
#5
Re: Clearing I/O stream
Worked like a charm! Thanks a ton.
-
February 10th, 2009, 05:17 PM
#6
Re: Clearing I/O stream
If you read it again, it indicates that you still need to use the str(newString) call to copy a new string into the stream. It doesn't say that the clear function will erase the actual string within the stream. Take another look and you will see that it is different from what you did.
You called clear() and then tried to use the operator<< to add new data. In the example, they called clear() followed by the str( newString ) function to replace the data that was in the stream previously. That is very easy to overlook as I have made the same mistake myself numerous times.
I must be honest though. In this scenario, I don't normally call clear at all. I guess it isn't a bad idea.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|