Jump to content
  • Advertisement

Archived

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

hallgeir

reading strings from std::istrstream to char* variables (memory allocation question)

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

Hey. Let''s say I have a std::istrstream object:
#include <strstream>

void Test(const char* s)
{
  std::istrstream stream(s);
  char *string1, string2;
  int stringlength1;
  int stringlength2;

  //then I want to "copy" the two first strings in the stream to the string1 and string2 variables. Well, that''s pretty straight forward, BUT is there possible to find the size of the next string in the stream, so I can allocate the correct amount of memory for the string1 and string2 variables?
  //so the question is simply: How do I find stringlength1 and stringlength2 (if it is possible)?

  string1 = new char[stringlength1];
  string2 = new char[stringlength2];
  stream >> string1 >> string2;
}
 
I hope anyone can help. Thanks in advance "The mistakes are all waiting to be made.

Share this post


Link to post
Share on other sites
Advertisement
Hi ziggwarth

strstreams are deprecated, you shouldnt use them. Use stringstreams instead, with std::string, it would solve both of your problems




#include<sstream>
#include<string>

int main(int argc, char *argv[])
{
std::string string1 = "hello world again";
std::string string2, string3;
std::istringstream istr(string1);
istr >> string1 >> string2 >> string3;
}


[edited by - Jingo on February 6, 2004 5:55:40 AM]

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.

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!