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

Started by
1 comment, last by hallgeir 20 years, 2 months ago
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.
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]
Ah!
I see. I didn''t know that.
Thanks a lot

"The mistakes are all waiting to be made.

This topic is closed to new replies.

Advertisement