osstream and stuff

G

grahamo

Hi,

Can anybody tell me why I sometimes see an integer value when I try to
print the "out" variable below and if I coerce it into a (char*), I
see the string "hello" as expected. The str() member function returns
a char* so I would have thout there was no coercing needed. ???

char* in = strdup("hello");
strstream input(in, strlen(in);
input << std::ends;


cout << "stream contains " << input.str() << endl;

char* out = strdup(output.str());

cout << "out is " << out << endl;
cout << "out is " << (char*) out << endl;



thanks a million


GrahamO
 
B

Buster

grahamo said:
Can anybody tell me why I sometimes see an integer value when I try to
print the "out" variable below and if I coerce it into a (char*), I
see the string "hello" as expected. The str() member function returns
a char* so I would have thout there was no coercing needed. ???

(i) What platform? What compiler? What version?
(ii) Post a complete program.
(iii) Don't use strstream, it's deprecated.
(iv) Don't use strdup, it's nonstandard.
 
C

Christopher Benson-Manica

grahamo said:
char* in = strdup("hello");

1) Use std::strings, not strdup().
2) const char *in="hello", not strdup().
3) If you insist on using strdup() anyway, realize that you are
responsible for free()'ing the memory when you're done with it.
strstream input(in, strlen(in);
input << std::ends;
cout << "stream contains " << input.str() << endl;
char* out = strdup(output.str());

You omitted your declaration of output - what is it? If output is an
ostringstream (or perhaps osstream), str() is a std::string - not a const
char *, which is what strdup expects.

char *out=strdup( output.str().c_str() );
cout << "out is " << out << endl;
cout << "out is " << (char*) out << endl;

If you quit trying to use strdup() and C-style strings, your life will
become easier.
 
J

John Harrison

grahamo said:
Hi,

Can anybody tell me why I sometimes see an integer value when I try to
print the "out" variable below and if I coerce it into a (char*), I
see the string "hello" as expected. The str() member function returns
a char* so I would have thout there was no coercing needed. ???

What the str() member function produces is of no relevance.
char* in = strdup("hello");
strstream input(in, strlen(in);
input << std::ends;


cout << "stream contains " << input.str() << endl;

char* out = strdup(output.str());

cout << "out is " << out << endl;
cout << "out is " << (char*) out << endl;

That should never make any difference because out already is char* so
casting it can make no difference. Suggest you produce a complete compilable
program that consistently produces this error. Then post it here.

john
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top