The classes in <strstream> are deprecated.

Discussion in 'C++' started by Immortal Nephi, Apr 8, 2010.

  1. I want to store data into char array. Look at my example below.

    #include <strstream>
    #include <sstream>

    using namespace std;

    int main()
    {
    char s[25];
    std::eek:strstream myString( s, sizeof( s ) );

    // std::eek:stringstream myString( s, sizeof( s ) );

    myString << "this is a test" << std::ends;

    char t[ 4 ] = { 1, 2, 3, 4 };
    myString.write( reinterpret_cast<char*>( t ), sizeof(char)*4 );

    return 0;
    }

    MSDN documentation says The classes in <strstream> are deprecated.
    Consider using the classes in <sstream> instead.

    I change from std::eek:strstream myString( s, sizeof( s ) ) to
    std::eek:stringstream myString( s, sizeof( s ) )

    Why myString is unable to store data into s array?
    Immortal Nephi, Apr 8, 2010
    #1
    1. Advertising

  2. Immortal Nephi

    Ian Collins Guest

    On 04/ 8/10 12:37 PM, Immortal Nephi wrote:
    > I want to store data into char array. Look at my example below.
    >
    > #include<strstream>
    > #include<sstream>
    >
    > using namespace std;
    >
    > int main()
    > {
    > char s[25];
    > std::eek:strstream myString( s, sizeof( s ) );
    >
    > // std::eek:stringstream myString( s, sizeof( s ) );
    >
    > myString<< "this is a test"<< std::ends;
    >
    > char t[ 4 ] = { 1, 2, 3, 4 };
    > myString.write( reinterpret_cast<char*>( t ), sizeof(char)*4 );
    >
    > return 0;
    > }
    >
    > MSDN documentation says The classes in<strstream> are deprecated.
    > Consider using the classes in<sstream> instead.


    They are correct, ostrstream is pre-standard.

    > I change from std::eek:strstream myString( s, sizeof( s ) ) to
    > std::eek:stringstream myString( s, sizeof( s ) )
    >
    > Why myString is unable to store data into s array?


    Because ostreams use a stringbuf to hold data.

    std::eek:stringstream myString( s, sizeof( s ) );

    creates myString and copies sizeof s bytes into it. You then add more
    data to the internal buffer, after the 25 bytes of garbage.

    You can see the string using myString.str().

    --
    Ian Collins
    Ian Collins, Apr 8, 2010
    #2
    1. Advertising

  3. Immortal Nephi wrote:
    > I want to store data into char array. Look at my example below.
    >
    > #include<strstream>
    > #include<sstream>
    >
    > using namespace std;
    >
    > int main()
    > {
    > char s[25];
    > std::eek:strstream myString( s, sizeof( s ) );
    >
    > // std::eek:stringstream myString( s, sizeof( s ) );
    >
    > myString<< "this is a test"<< std::ends;
    >
    > char t[ 4 ] = { 1, 2, 3, 4 };
    > myString.write( reinterpret_cast<char*>( t ), sizeof(char)*4 );


    The reinterpret_cast here is superfluous, and sizeof(t) would be less of
    a maintenance hazard...

    >
    > return 0;
    > }
    >
    > MSDN documentation says The classes in<strstream> are deprecated.
    > Consider using the classes in<sstream> instead.
    >
    > I change from std::eek:strstream myString( s, sizeof( s ) ) to
    > std::eek:stringstream myString( s, sizeof( s ) )
    >
    > Why myString is unable to store data into s array?


    ostringstream differs from ostrstream in that it manages the accumulated
    string for you, as a std::string. This means that you no longer have to
    worry about guessing how long the buffer needs to be. This is a very
    good thing, and it's why ostrstream is now deprecated.

    Your example might look something like this using an ostringstream:
    int main(int argc, char *argv[])
    {
    std::eek:stringstream buff;
    buff << "This is a test";
    char t[4] = {1, 2, 3, 4};
    buff.write(t, sizeof(t));
    return 0;
    }

    To get back the std::string containing the formatted output, call buff.str(). If
    you *really* need to get at the raw characters in the string, you can then call
    std::string::c_str(), but thinking in terms of std::strings instead of C-style
    strings will make your life a lot easier and less accident-prone.

    James
    James Lothian, Apr 8, 2010
    #3
  4. On Apr 7, 8:17 pm, James Lothian
    <> wrote:
    > Immortal Nephi wrote:
    > >    I want to store data into char array.  Look at my example below.

    >
    > > #include<strstream>
    > > #include<sstream>

    >
    > > using namespace std;

    >
    > > int main()
    > > {
    > >    char s[25];
    > >    std::eek:strstream myString( s, sizeof( s ) );

    >
    > > // std::eek:stringstream myString( s, sizeof( s ) );

    >
    > >    myString<<  "this is a test"<<  std::ends;

    >
    > >    char t[ 4 ] = { 1, 2, 3, 4 };
    > >    myString.write( reinterpret_cast<char*>( t ), sizeof(char)*4 );

    >
    > The reinterpret_cast here is superfluous, and sizeof(t) would be less of
    > a maintenance hazard...


    Why do you say that? reinterpret_cast is always used in read()
    function and write() function. I am instructed to follow it by Deitel
    How to program C++ book.
    What happen if all characters are stored in raw memory? The raw
    memory has data type, which is char. The data with char data type is
    filled with float data type. You must convert from char to float and
    you won’t lose precision.
    Read() function takes care to read each character from file and
    copies to the memory. It captures four characters and then is
    converted to float data. All floating numbers are preserved from four
    characters.

    > >    return 0;
    > > }

    >
    > >    MSDN documentation says The classes in<strstream>  are deprecated.
    > > Consider using the classes in<sstream>  instead.

    >
    > >    I change from std::eek:strstream myString( s, sizeof( s ) ) to
    > > std::eek:stringstream myString( s, sizeof( s ) )

    >
    > >    Why myString is unable to store data into s array?

    >
    > ostringstream differs from ostrstream in that it manages the accumulated
    > string for you, as a std::string. This means that you no longer have to
    > worry about guessing how long the buffer needs to be. This is a very
    > good thing, and it's why ostrstream is now deprecated.
    >
    > Your example might look something like this using an ostringstream:
    > int main(int argc, char *argv[])
    > {
    >      std::eek:stringstream buff;
    >      buff << "This is a test";
    >      char t[4] = {1, 2, 3, 4};
    >      buff.write(t, sizeof(t));
    >      return 0;
    >
    > }
    >
    > To get back the std::string containing the formatted output, call buff.str(). If
    > you *really* need to get at the raw characters in the string, you can then call
    > std::string::c_str(), but thinking in terms of std::strings instead of C-style
    > strings will make your life a lot easier and less accident-prone.


    You are right. Sometimes, I need c-style string. I have my
    responsibility to provide character sizes. Not a problem. I would
    prefer to use vector instead.
    Immortal Nephi, Apr 8, 2010
    #4
  5. Immortal Nephi

    Jonathan Lee Guest

    On Apr 7, 9:42 pm, Immortal Nephi <> wrote:
    > On Apr 7, 8:17 pm, James Lothian
    > > Immortal Nephi wrote:
    > > >    char t[ 4 ] = { 1, 2, 3, 4 };
    > > >    myString.write( reinterpret_cast<char*>( t ), sizeof(char)*4 );

    >
    > > The reinterpret_cast here is superfluous, and sizeof(t) would be less of
    > > a maintenance hazard...

    >         Why do you say that?


    Because t is a char array. The conversion to char* is automatic. There
    is truly no need for a reinterpret_cast.

    Also, sizeof(char) is always 1.

    --Jonathan
    Jonathan Lee, Apr 8, 2010
    #5
  6. Immortal Nephi

    Ian Collins Guest

    On 04/ 8/10 01:42 PM, Immortal Nephi wrote:
    > On Apr 7, 8:17 pm, James Lothian
    > <> wrote:
    >>
    >>> char t[ 4 ] = { 1, 2, 3, 4 };
    >>> myString.write( reinterpret_cast<char*>( t ), sizeof(char)*4 );

    >>
    >> The reinterpret_cast here is superfluous, and sizeof(t) would be less of
    >> a maintenance hazard...

    >
    > Why do you say that? reinterpret_cast is always used in read()
    > function and write() function. I am instructed to follow it by Deitel
    > How to program C++ book.


    As was explained else-thread, char t[4] decays to (effectively is) a char*.

    > What happen if all characters are stored in raw memory? The raw
    > memory has data type, which is char. The data with char data type is
    > filled with float data type. You must convert from char to float and
    > you won’t lose precision.


    I think you are a confusing conversion as casting. If you have a 4 byte
    float type and you want to access it as any array of 4 chars, then you
    cast the address to char*.

    float f;
    char* p = reinterpret_cast<char*>(&f);

    If you want to represent the value of f as a char, you use (an ill
    advised) conversion:

    char c = f;

    > Read() function takes care to read each character from file and
    > copies to the memory. It captures four characters and then is
    > converted to float data. All floating numbers are preserved from four
    > characters.


    Cast to a float. On some systems, sizeof(float) may not be 4.

    >> To get back the std::string containing the formatted output, call buff.str(). If
    >> you *really* need to get at the raw characters in the string, you can then call
    >> std::string::c_str(), but thinking in terms of std::strings instead of C-style
    >> strings will make your life a lot easier and less accident-prone.

    >
    > You are right. Sometimes, I need c-style string. I have my
    > responsibility to provide character sizes. Not a problem. I would
    > prefer to use vector instead.


    If you want to have control of the raw data use for an ostream, you can
    write your own streambuf class. But that is not something I'd recommend
    for a novice. It is however an excellent exercise for a more
    experienced programmer.

    --
    Ian Collins
    Ian Collins, Apr 8, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Barney Barumba
    Replies:
    0
    Views:
    552
    Barney Barumba
    Jul 23, 2003
  2. Bpb

    strstream question

    Bpb, Sep 23, 2003, in forum: C++
    Replies:
    1
    Views:
    398
    Ron Natalie
    Sep 23, 2003
  3. vishnu mahendra

    Use of strstream.h?

    vishnu mahendra, Apr 6, 2004, in forum: C++
    Replies:
    2
    Views:
    1,548
    Kevin Goodsell
    Apr 6, 2004
  4. vishnu mahendra

    Queries about strstream.h

    vishnu mahendra, Apr 10, 2004, in forum: C++
    Replies:
    3
    Views:
    4,368
    Buster
    Apr 11, 2004
  5. News For Mohan

    use of strstream object of standard lib

    News For Mohan, Aug 10, 2004, in forum: C++
    Replies:
    1
    Views:
    389
    Victor Bazarov
    Aug 10, 2004
Loading...

Share This Page