strstream: output is garbled

This is a discussion on strstream: output is garbled within the C++ Programming forums, part of the General Programming Boards category; When using strstream, my output is garbled. Here is the code: Code: strstream strTemp; strTemp << "hello" << endl; pDC->TextOut(0,0,strTemp.str()); ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    strstream: output is garbled

    When using strstream, my output is garbled. Here is the code:
    Code:
    strstream strTemp;
    strTemp << "hello" << endl;
    pDC->TextOut(0,0,strTemp.str());
    The output I get is 'hello' followed immediately by a bunch of rubbish characters. How can I stop this?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    all strings need to end with '\0'...try

    Code:
    len = strlen(strTemp);
    strTemp[len] = '\0';
    simple is always an understatement.....

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You may be using the older string stream object as defined in <strstream>. I think I remember having problems like this long ago once when trying to use these objects. I finally figured out that I needed to add an end-of-stream character at the very end. I haven't done it the way sweets has but I think this accomplishes the same thing... haven't tried it though but I think this is what I did.
    Code:
    strstream strTemp;
    strTemp << "hello" << endl << ends;
    pDC->TextOut(0,0,strTemp.str());
    Using the newer string stream objects defined in <sstream> may prevent this from being necessary.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    It's not really "new" vs. "old"...
    <strstream> is for char buffers, not just C-strings. That's why you need to provide the ends explicitly.
    <sstream> stringstream, istringstream, and ostringstream are the formal I/O streams for std::string.

    The old stuff would be in a header with a ".h" at the end of it

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  3. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 02:29 PM
  4. Problems with output formatting
    By supaben34 in forum C++ Programming
    Replies: 0
    Last Post: 11-22-2002, 10:22 PM
  5. Removing *Unchanged* Output File (ofstream) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 01-05-2002, 06:47 PM

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