Four questions on std::stringstream

This is a discussion on Four questions on std::stringstream within the C++ Programming forums, part of the General Programming Boards category; Greetings everyone! I have a few hopefully pretty easy questions on std::stringstream 1) How do you "reset" a stringstream so ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Four questions on std::stringstream

    Greetings everyone! I have a few hopefully pretty easy questions on std::stringstream

    1) How do you "reset" a stringstream so that it can be used again? I looked around on Google and found ss.str(""); (where ss is an object of type std::stringstream) but this seems like an ugly hack and I was wondering if there was a "cleaner" way to do it.

    2) Is it possible to get a C style string from a stringstream? I tried ss.str().c_str() but I don't think that works.

    3) How can you get the length of a stringstream? Does ss.str().length() work?

    4) In the Visual Studio debugger, how can I display the contents of a stringstream? the Watch window can't seem to parse ss.str().c_str().

    Thanks for the help.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    1) str("") is the only way I know of
    2) that's what I usually use
    3) yes works
    4) What is a Visual debugger
    Kurt

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Did you try testing 1 through 3? It's the best way to test things. What did it do in the testing?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but this seems like an ugly hack and I was wondering if there was a "cleaner" way to do it.
    The funny thing is that a lot of C++ conventions look like ugly hacks.

    >2) Is it possible to get a C style string from a stringstream?
    It depends on what you want it for.

    >I tried ss.str().c_str() but I don't think that works.
    How so?

    >3) How can you get the length of a stringstream? Does ss.str().length() work?
    Have you tried it?

    >the Watch window can't seem to parse ss.str().c_str().
    You should be able to dig down to the base string while watching ss.
    My best code is written with the delete key.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    2) ss.str().c_str() will work, but only as long as the temporary std::string stays alive. Which is long enough to pass the c-string to a function (like strcpy), but not more than that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,523
    Quote Originally Posted by CornedBee
    2) ss.str().c_str() will work, but only as long as the temporary std::string stays alive.
    This is true, but the constraint is more general than that. The string itself needs to remain unmodified. So
    Code:
         std::string some_string(some_content);
         const char *p = some_string.c_str();
         
         //   some operation that modifies the content of some_string()
    
        strcpy(some_buffer, p);    // undefined behaviour as p is effectively a dangling reference
    ss.str() yields a temporary string with limited lifetime, and ss.str().c_str() is only valid as long as that temporary remains alive. Being destructed, so it is no longer alive, is a rather significant modification of that temporary string.

    Quote Originally Posted by CornedBee
    Which is long enough to pass the c-string to a function (like strcpy), but not more than that.
    The other obvious caveat is that the c_str() method returns a const pointer, which is a fairly broad hint you're not supposed to modify the data it points at. So
    Code:
        char x[10];
        strcpy(x, ss.str.c_str());
    is OK (as long as the length of the string with terminating zero does not exceed 9) but this;
    Code:
        char x[] = "Hello";
        strcpy(ss.str.c_str(), x);
    will not work; the compiler will complain bitterly as the first argument of strcpy() is required to be non-const.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 08:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 08:22 AM

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