Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > The classes in <strstream> are deprecated.

Reply
Thread Tools

The classes in <strstream> are deprecated.

 
 
Immortal Nephi
Guest
Posts: n/a
 
      04-08-2010
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:strstream myString( s, sizeof( s ) );

// std: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:strstream myString( s, sizeof( s ) ) to
std:stringstream myString( s, sizeof( s ) )

Why myString is unable to store data into s array?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-08-2010
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:strstream myString( s, sizeof( s ) );
>
> // std: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:strstream myString( s, sizeof( s ) ) to
> std:stringstream myString( s, sizeof( s ) )
>
> Why myString is unable to store data into s array?


Because ostreams use a stringbuf to hold data.

std: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
 
Reply With Quote
 
 
 
 
James Lothian
Guest
Posts: n/a
 
      04-08-2010
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:strstream myString( s, sizeof( s ) );
>
> // std: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:strstream myString( s, sizeof( s ) ) to
> std: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: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
 
Reply With Quote
 
Immortal Nephi
Guest
Posts: n/a
 
      04-08-2010
On Apr 7, 8:17*pm, James Lothian
<(E-Mail Removed)> 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:strstream myString( s, sizeof( s ) );

>
> > // std: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:strstream myString( s, sizeof( s ) ) to
> > std: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: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.
 
Reply With Quote
 
Jonathan Lee
Guest
Posts: n/a
 
      04-08-2010
On Apr 7, 9:42*pm, Immortal Nephi <(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-08-2010
On 04/ 8/10 01:42 PM, Immortal Nephi wrote:
> On Apr 7, 8:17 pm, James Lothian
> <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes within classes David ASP .Net 2 07-22-2005 07:13 PM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM
Modifiers applied to attributes, local variables, member functions, classes and inncer classes ! Razvan Java 11 07-17-2004 08:57 PM
Can I using reflection to get all child classes or classes undera package dynamically? Carfield Yim Java 1 05-31-2004 05:33 PM
How to access inner classes variables & methods from outer classes lonelyplanet999 Java 1 11-13-2003 01:54 PM



Advertisments