How can I convert istream * to a string or just print it?

Below is the functional part of the / C ++ connector, it returns istream *. if I just try and print it, it will display a hexadecimal or memory location, because its type is *.

istream *stream = res->getBlob(1); 

I tried to read and print it as follows:

  string s; while (getline(*stream, s)) cout << s << endl; 

But this happens with access violation. any other way i can print it or convert to string?

stream value before getline:

  • stream 0x005f3e88 {_Chcount = 26806164129143632} std :: basic_istream> *

therefore it seems that this is really for me. I think it will be null or 0 if it fails

+4
source share
2 answers

You can extract and print std::istream using your stream buffer:

 std::cout << in->rdbuf(); 

Of course, this will consume the input, and you cannot get it again. If you want to save the content, you can write std::ostringstream and print the contents using the str() method. Alternatively, you can directly build std::string from a stream, for example:

 std::string content{ std::istreambuf_iterator<char>(*in), std::istreambuf_iterator<char>() }; 

By the way, when you printed the stream pointer, you actually used the output operator for void const* : it prints the address that the pointer refers to. In C ++ 03, you can even restore an appropriately printed pointer by reading void* using std::istream : while the object marked on the object has not been deleted, you can get the pointer back this way! However, in C ++, hiding the pointer is forbidden to support additional garbage collection, which may or may not be added to the language in the future. However, guaranteeing non-hidden pointers also helps member debuggers.

+6
source

You can use the std :: getline function in a while loop to display data in istream. Here is an example that I ran and it worked correctly:

 #include <iostream> #include <sstream> #include <istream> #include <string> int main() { std::stringstream s1("This is a test string\nWith two lines"); std::istream s2(s1.rdbuf()); //just creates the istream to start with std::string stt; while(std::getline(s2,stt)) //can also have delimiter in getline { std::cout<<stt<<std::endl; } return 0; } 

Run it and it will display:

 This is a test string With two lines 

I also tried this to use an istream pointer like yours:

 #include <iostream> #include <sstream> #include <istream> #include <string> int main() { std::stringstream s1("This is a test string\nWith three lines); std::istream s2(s1.rdbuf()); //just creates istream to start with std::istream *s3 = &s2; //and use a pointer to istream like code at top std::string stt; while(std::getline(*s3,stt,'\n')) { std::cout<<stt<<std::endl; //result. } return 0; } 

This code worked and gave the same result as me, without using a pointer. I could not reproduce your error. So the problem looks like creating your istream (e.g. istream * stream = res-> getBlob (1);).

+1
source

Source: https://habr.com/ru/post/1500633/


All Articles