CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Posts
    7

    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!

  2. #2
    Join Date
    Feb 2009
    Posts
    7

    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.

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    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.

  4. #4
    Join Date
    Feb 2009
    Posts
    7

    Re: Clearing I/O stream

    I must have misread this, then:

    http://www.cppreference.com/wiki/io/sstream/str

  5. #5
    Join Date
    Feb 2009
    Posts
    7

    Re: Clearing I/O stream

    Worked like a charm! Thanks a ton.

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    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
  •  





Click Here to Expand Forum to Full Width

Featured