Jump to content
  • Advertisement
Sign in to follow this  
ordered_disorder

I'm looking for a good strstream tutorial

This topic is 4865 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

I've been programming in c++ for a while and for the whole time I've been using atoi, and sprintf for changing characters into their numerical equivalent, I thought that was the c++ way of doing things. Shows how much I know! My friend told me to use strstream if I want to be doing 100% c++ which is what I want to do, but I've been having a lot of trouble finding any tutorials for strstream on google. Also MSDN didn't give me very much information. Do any of you gnetters out there know of any good strstream tutorials? Or can you just give me a few examples, such as turning numerical char into integers or their hexadecimal equivalents? Thank you for your time.

Share this post


Link to post
Share on other sites
Advertisement
Technically there is no such thing as strstream, its deprecated and pre-dates C++ standardization so avoid it. This is probably why you can't find much info on them, you want to look into:

std::basic_istringstream, std::basic_ostringstream std::basic_stringstream,

and the convient type aliases for char & wchar_t:

std::istringstream, std::wistringstream,
std::ostringstream, std::wostringstream
std::stringstream, std::wstringstream

All provided in header sstream.

Regarding online tutorials i don't think there is one or a decent one on string streams and to be honest i don't think you need one, just a good reference. If you know how to use C++ I/O streams then you'll have no problem with string streams. You do need to be aware of (you don't have to use) std::basic_string (and type aliases std::string/wstring) for string streams aswell.

Share this post


Link to post
Share on other sites
What you are looking for is stringstream. Here's a reference: clicky!. And here's some examples of how to convert numbers to string and the other way round. Happy converting!

Edit: Doh! To slow.

Share this post


Link to post
Share on other sites
if you want to turn chars in to number subtract '0' or 0x30 a numbers start )x30 in ascii table

ie



int char_to_int (char c)
{
return c - 0x30;
}


int str_to_int(char * str)
{
int result= 0;
int mul = 1;

for(int x = strlen(str)-1; x ==0 ;x++)
{
result = result * (char_to_int(str[x])*mul);
mul *=10;
}

return result;

}


this will covert the char to int going right to left and then multipiles by their ten^n values

ie the string "1234" will work as

0 +((0x34 - 0x30) * 1) = 4 (first iteration)
4 +((0x33 - 0x30) * 10) = 34 (2nd iteration)
34 +((0x32 - 0x30) * 100) = 234 (3rd iter)
234 +((0x31 - 0x30) * 1000) = 1234 (4th iter)

and so on so on

a good reference for C++ in general is "the C++ programing language" 3rd by bjarne stroustrup it pretty much everything there is to know about C++

hope this helps

andrew

Share this post


Link to post
Share on other sites
And something that never seems to show in the tutorials, but is important to know:

Usually, you stuff things into the stringstream, and then push them to a string:


stringstream ss;
string st;

ss << "moo " << 1 << " cow.";
st = ss.str();


And then, you often want to do the same thing again:


...
ss << "oink " << 2 << " pigs.";
st = ss.str();


oops. st is now: moo 1 cow.oink 2 pigs.

Annoying!

Anyways, there's no clear solution for this in the tutorials I've seen. Plus the 'clear' function doesn't do what you expect [annoying!!!]

This does:


ss.str("");


Which simply resets the contained string to 0 length, allowing you to reuse the stream.

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Technically there is no such thing as strstream, its deprecated and pre-dates C++ standardization so avoid it.


Not true. Though deprecated, it is a normative part of the C++ standard.

In any case, for string conversions, I recommend the use of boost::lexical_cast. It sidesteps all the nasty issues you can run into trying to manually use streams for conversions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!