Convert ostream to standard string

I am very new to C ++ STL, so this may be trivial. I have an ostream variable with some text in it.

 ostream* pout; (*pout) << "Some Text"; 

Is there a way to extract a stream and store it in a string of type char* ?

+47
c ++ iostream stl
Aug 18 2018-10-18
source share
3 answers
  std::ostringstream stream; stream << "Some Text"; std::string str = stream.str(); const char* chr = str.c_str(); 

And I will explain what happens in the answer to this question , which I wrote not an hour ago.

+45
Aug 18 '10 at 14:35
source share

The question was about ostream for a string, not ostringstream for a string.

For those interested in answering the actual question ( ostream specific), try the following:

 void someFunc(std::ostream out) { std::stringstream ss; ss << out.rdbuf(); std::string myString = ss.str(); } 
+110
Jul 22 '13 at 21:34
source share

Try std::ostringstream

  std::ostringstream os; os<<"Hello world"; std::string s=os.str(); const char *p = s.c_str(); 
+3
Aug 18 '10 at 14:37
source share



All Articles