The classes in <strstream> are deprecated.

I

Immortal Nephi

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?
 
I

Ian Collins

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().
 
J

James Lothian

Immortal said:
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
 
I

Immortal Nephi

Immortal said:
   I want to store data into char array.  Look at my example below.

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.
 
J

Jonathan Lee

   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?[/QUOTE]

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
 
I

Ian Collins

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.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top