Question

The below function part of connector/C++, it returns a istream*. if i just try and print it, it shows hex or a memory location because its a * type.

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

I tried to read & print it with this:

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

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

the value of stream before the getline:

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

so it seems that its valid to me. I think it would be null or 0 if it failed

Was it helpful?

Solution

You can extract and print a std::istream by using its stream buffer:

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

Of course, this will consume the input and you may not be able to get it again. If you want to keep the content, you could write it an std::ostringstream instead and print the content using the str() method. Alternatively, you can directly construct a std::string from a stream, too, e.g.:

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

BTW, when you printed your stream pointer, you actually used the output operator for void const*: it prints the address the pointer is referring to. In C++03 you could even restore a correspondingly printed pointer by reading a void* using an std::istream: as long as the pointed to object wasn't deleted, you could get a pointer back that way! In C++11 pointer hiding is prohibited, however, to support optional garbage collection which may or may not be added to the language in the future. The guarantee about non-hidden pointers also helps member debuggers, though.

OTHER TIPS

You can use the std::getline function in the while loop to display the data in the 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 displays:

This is a test string
With two lines

I also tried this so that I was using pointer to the istream like you have:

#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 ran and gave the same result as I got without using the pointer. I was unable to reproduce your error. So the problem looks like the creation of your istream (e.g. istream *stream = res->getBlob(1);).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top