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

14,942

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.

Share:
14,942
user1397417
Author by

user1397417

Updated on June 20, 2022

Comments

  • user1397417
    user1397417 almost 2 years

    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

  • user1397417
    user1397417 over 10 years
    the line std::cout << in->rdbuf(); still crashes for me when i replace it with my while getline attemp. both crash the same way with an access violation. And I dont understand much else of your answer to try the other ways you mentioned
  • Dietmar Kühl
    Dietmar Kühl over 10 years
    @user1397417: I didn't see that it actually crashes and answered the question on the title. The other approach will probably crash pretty much the same way. It implies that stream was probably destroyed by the time you try to use it! What happens between you getting stream and using it? I guess you invalidated the result, e.g., by moving to the next element or keeping the stream for later.
  • Dietmar Kühl
    Dietmar Kühl over 10 years
    @user1397417: What is the database type of the field you try to access?
  • user1397417
    user1397417 over 10 years
    there is NOTHING inbetween my two blocks of code. the database structure of the column selected is 'english varchar(225) utf8_bin '
  • Dietmar Kühl
    Dietmar Kühl over 10 years
    @user1397417: Looking a the source, the function getBlob() actually returns a new'ed std::istringstream (you'll need to make sure that it is deleted after you used assuming the use gets sorted...). One things I could imagine is that the standard C++ library used for the MySQL connector and for your build are actually different: if you should verify that both are build with exactly the same version of the standard C++ library.
  • user1397417
    user1397417 over 10 years
    i uninstalled everything mysql, and re downloaded latest versions tried to build again. it said something about not finding a boost library so i installed boost and then it build and ran without crashing. seems to work fine now.