Results 1 to 11 of 11

Thread: ostrstream problems

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    ostrstream problems

    how do you clear or empty an ostrstream?
    Code:
    #include <windows.h>
    #include <strstream>
    
    using namespace std;
    
    void main(){
    
    	ostrstream oss;
    
    	oss << "3.14159265359" << ends;
    
    	MessageBox(0, oss.str(), "", 0);
    
    
    	// now clear out the stream so a new stream can be written
    
    
    	oss << "1.41421356237" << ends; 
    
    	MessageBox(0, oss.str(), "", 0);
    
    }

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    int main() {
    	ostringstream oss;
    
    	oss << "Hi there: " << 5 << " " << 10 << ends;
    	cout << oss.str() << endl;
    
    	oss.seekp(0);
    
    	oss << "Again!! " << "blah: " << 5 * 10 << ends;
    	cout << oss.str() << endl;
    
    	return 0;
    }
    ....with small modifications to bring you up to the standard
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Oh, right. stringstreams are the new standard.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Less memory-leak potential since they return a string, not char* (in the old ones, when you call .str(), it locks the buffer and strange things happen).

    Plus, these are all templated (w00t!).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    So are you saying oss.str().c_str() may cause a memory leak too?

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No, because it returns a constant pointer, and doesn't lock any buffers since it will delete it when the destructor is called.

    Hence why you should use the new ones - as Bjarne says, "Resource Initialisation is Acquisition"
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Cool.
    I guess.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Thumbs up

    Thanks.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What does that mean in easy English parksie?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by CornedBee
    What does that mean in easy English parksie?
    What, the acquisition thing?

    It basically means that once you create a representation of something, you have allocated what it is, for example:
    Code:
    class mutex { };
    
    void lockedcode() {
        mutex m; // blocks until it can recieve the mutex
    
        // some unsafe operation
    }
    In this code, creating the mutex object tries to acquire the OS's mutex handle, and destroying it releases it again.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Ok, I think I understand...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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