Trouble with strstream

This is a discussion on Trouble with strstream within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get strstream to work and its not working right. Both these code output stuff to a string ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    648

    Trouble with strstream

    I'm trying to get strstream to work and its not working right. Both these code output stuff to a string except that it doesn't put a null so the output just keeps going until a zero. I'm using unmanaged VC.NET.
    Code:
    char buffer[1024];
    ostrstream s(buffer, sizeof(buffer);
    s << "hey" << uppercase << hex << 255 << endl;
    
    // will work with this here, but it should even without:
    // str[s.pcount()]=0;
    
    cout << s.str();
    Code:
    strstream s;
    s << "hey" << uppercase << hex << 255 << endl;
    cout << s.str();
    Output of both codes (nothing else is printed):
    Code:
    heyFF
    ══════════════════════════께께슬슬슬슬ε■ε■

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Nevermind, I found the solution. To end a string stream, you must place a ends at the end (end string). That way the string will be terminated by a zero. So this works:
    Code:
    ostrstream s;
    s << "hey" << uppercase << hex << 255 << ends;
    cout << s.str();
    <posting because someone might wanna search this topic later>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 11:44 AM
  2. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  4. strstream: output is garbled
    By bennyandthejets in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2004, 07:24 AM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2003, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21