Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

andyZER0

ostringstream

This topic is 5156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way to make a char* (or char string) equal to a ostringstream.str()? In the tutorial below, a "string" is equal to an ostringstream.str(). That''ll obviously compile, but I tried making a "char *mystr = oss.str()" and I get errors with that. I swear I''ve seen this done with a char string on this forum...or one of the many other forums here! But I forgot to bookmark it or something--I never found it again Thanks in advance. #include <iostream> #include <sstream> #include <string> using namespace std; int main () { ostringstream oss; string mystr; oss << "Sample string"; mystr=oss.str(); // <---- RIGHT HERE cout << mystr; return 0; }

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
well , I do not know whether u can type caste , but if a
char* is all that u want then u can very well do that

char * mystr
mystr = oss.str().c_str() ;

This should work fine...

Share this post


Link to post
Share on other sites
No, that won''t because std::string::c_str() returns a const char *, and if you make mystr a const char *, it will point to deallocated memory once the ostringstream goes out of scope. If you have some grudge against using std::string, then you should do:

char mystr[256];
strcpy(mystr, oss.str().c_str());

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!