How to use basic_istream when you have a std::string - c++

I have a function that uses basic_istream as an argument, and I have a std::string with the data I need to pass it. How do I do that?

You can put the string data into a stream:
std::string x;
std::stringstream ss(x); // put string into stream
function_taking_stream(ss);

A full working example showing how to parse a json string using boost property_tree:
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using boost::property_tree::ptree;
int main ()
{
std::string jsonString ("{ \"my key\": \"my value\" }");
ptree pt;
std::stringstream ss(jsonString);
read_json(ss, pt);
std::cout << pt.get<std::string>("my key") << std::endl;
return 0;
}

I have a function that uses basic_istream as an argument...
So I'm assuming you have a function that accepts a variable of a type specialized from the basic_istream template.
...and I have a std::string with the data I need to pass it.
So I'm assuming you want to feed this data into the istream argument.
How do I do that?
You don't. You can't feed output to an input stream!

I think the OP means he has a basic_istream<E> to read from and wants to read to a std::string, not to a std::basic_string<E>.
You should probably write to the basic_string first then convert that to a std::string. If you want to ensure the conversion is correct you probably want a proper UTF-8 to UTF-16 converter.
If you know you are dealing with only ASCII bytes you can do something like:
std::basic_string<E> strTemp;
is >> strTemp;
std::string str( strTemp.begin(), strTemp.end() );
If you have a basic_ostream<E> you can similarly first construct a basic_string<E> from your std::string and then write that to the stream.
You can "specialise" your template function so that if E happens to be char the conversion is quick. For that purpose you can input the two strings as non-const references and the specialised version could just "swap".

Related

can I std::find a string in a stringstream?

I have a stringstream that I'd like to iterate and determine if a substring exists in it.
I know that I could just convert to a string and do std::string::find(), but I was just hoping to avoid the conversion from stringstream to string if possible.
I understand the following won't work because the istream_iterator uses char as its type (not string)
stringstream ssBody;
string sFindThis;
...
auto itr = std::find (
istreambuf_iterator<char>(ssBody),
istreambuf_iterator<char>(),
sFindThis
);
But can I somehow search for a string in stringstream with std::find or similar without a conversion to string?
The C++ standard does not define any std::[io]?stringstream methods for searching its contents.
Neither can you use std::istreambuf_iterator together with std::search(), since std::istreambuf_iterator is an input iterator, but std::search() requires a forward iterator.
The only effective way to search a string stream is to convert it to a std::string, first.
using pubsetbuf it is possible to associate a buffer with basic_stringbuf member and then search the buffer, however behavior of this function is implementation defined. explanations and the example are from http://en.cppreference.com/w/cpp/io/basic_stringbuf/setbuf
#include <iostream>
#include <sstream>
int main()
{
std::ostringstream ss;
char c[1024] = {};
ss.rdbuf()->pubsetbuf(c, 1024);
ss << 3.14 << '\n';
std::cout << c << '\n';
}

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

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
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.
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);).

Difference stringstream and ostringstream

I was trying out the below snippet but it is not giving the desired output:
#include<iostream>
#include<sstream>
using namespace std;
void MyPrint(ostream& stream)
{
cout<<stream.rdbuf()<< endl;
}
int main()
{
stringstream ss;
ss<<"hello there";
MyPrint(ss); //Prints fine
ostringstream oss;
oss<<"hello there";
MyPrint(oss); //Does not print anything
getchar();
}
I am aware that the only possible differences between stringstream and ostringstream is that the later forces the direction and is a bit faster than stringstream.
Am I missing out on anything?
PS: A similar question was posted earlier but didn't get any answers.
std::stringstream and std::ostringstream pass different
flags to the std::stringbuf. In particular, the
std::stringbuf of an std::ostringstream does not support
reading. And std::cout << stream.rdbuf() is a read operation
on the streambuf.
The way to extract characters from an std::ostringstream is by
using the std::ostringstream::str() function.
stringstream shouldn't be thought of as a bidirectional implementation of ostringstream and istringstream. It is implemented as a derrived class of both ostringstream and istringstream, which is why it implements both input and output capabilities.
Choosing which one to use depends on what it is used for. If you only need to write data to it on a stream without being able to access the data via a stream, then an ostringstream is all you would need. However, if you want to implement bidirectional on something you provide to an API but restrict it, you can cast it:
stringstream ss; // My bidirectional stream
ostringstream *p_os = &ss; // Now an output stream to be passed to something only allowed to write to it.
int bytes = collectSomeData(p_oss);

How do I use write with stringstream?

I have a vector<char> of data which I want to write into std::stringstream.
I tried:
my_ss.write(vector.data(), vector.size());
...but it seems to put nothing into my_ss which I declared as follows:
std::stringstream my_ss( std::stringstream::binary);
Why write is not working (app does not crash and compiles with 0 errors, 0 warnings)?
For the "how do I do it" you can use a std::ostream_iterator:
std::copy(vector.begin(), vector.end(), std::ostream_iterator<char>(my_ss));
Complete example:
#include <iterator>
#include <sstream>
#include <vector>
#include <iostream>
#include <algorithm>
int main() {
std::vector<char> vector(60, 'a');
std::ostringstream my_ss;
std::copy(vector.begin(), vector.end(), std::ostream_iterator<char>(my_ss));
std::cout << my_ss.str() << std::endl;
}
You could also just use that to construct a string directly, without going via a stringstream at all:
std::string str(vector.begin(), vector.end()); // skip all of the copy and stringstream
Though you haven't given any code, it sounds like you probably just wrote:
std::stringstream my_ss (std::stringstream::binary);
If you wish to write to a stringstream you need to combine the flag std::stringstream::out in the constructor. If I'm right, then you would see things working fine if you changed this to:
std::stringstream my_ss (std::stringstream::out | std::stringstream::binary);
(Obviously if you wish to read from that stringstream you need to add std::stringstream::in)
UPDATE Now that you've given your code...yup, this is your specific problem. Note #awoodland's point about the fact that you can just construct a string from a vector of chars instead (if that's the only thing you were planning on doing with this stream.)
The default parameter for the mode of stringbuf in stringstream is out|in.
explicit basic_stringstream(ios_base::openmode _Mode =
ios_base::in | ios_base::out)
: _Mybase(&_Stringbuffer),
_Stringbuffer(_Mode)
{ // construct empty character buffer
}
You need to add stringstream::out if you pass something explicitly like stringstream:binary
Or just use std::ostringstream

How to read and write a STL C++ string?

#include<string>
...
string in;
//How do I store a string from stdin to in?
//
//gets(in) - 16 cannot convert `std::string' to `char*' for argument `1' to
//char* gets (char*)'
//
//scanf("%s",in) also gives some weird error
Similarly, how do I write out in to stdout or to a file??
You are trying to mix C style I/O with C++ types. When using C++ you should use the std::cin and std::cout streams for console input and output.
#include <string>
#include <iostream>
...
std::string in;
std::string out("hello world");
std::cin >> in;
std::cout << out;
But when reading a string std::cin stops reading as soon as it encounters a space or new line. You may want to use std::getline to get a entire line of input from the console.
std::getline(std::cin, in);
You use the same methods with a file (when dealing with non binary data).
std::ofstream ofs("myfile.txt");
ofs << myString;
There are many way to read text from stdin into a std::string. The thing about std::strings though is that they grow as needed, which in turn means they reallocate. Internally a std::string has a pointer to a fixed-length buffer. When the buffer is full and you request to add one or more character onto it, the std::string object will create a new, larger buffer instead of the old one and move all the text to the new buffer.
All this to say that if you know the length of text you are about to read beforehand then you can improve performance by avoiding these reallocations.
#include <iostream>
#include <string>
#include <streambuf>
using namespace std;
// ...
// if you don't know the length of string ahead of time:
string in(istreambuf_iterator<char>(cin), istreambuf_iterator<char>());
// if you do know the length of string:
in.reserve(TEXT_LENGTH);
in.assign(istreambuf_iterator<char>(cin), istreambuf_iterator<char>());
// alternatively (include <algorithm> for this):
copy(istreambuf_iterator<char>(cin), istreambuf_iterator<char>(),
back_inserter(in));
All of the above will copy all text found in stdin, untill end-of-file. If you only want a single line, use std::getline():
#include <string>
#include <iostream>
// ...
string in;
while( getline(cin, in) ) {
// ...
}
If you want a single character, use std::istream::get():
#include <iostream>
// ...
char ch;
while( cin.get(ch) ) {
// ...
}
C++ strings must be read and written using >> and << operators and other C++ equivalents. However, if you want to use scanf as in C, you can always read a string the C++ way and use sscanf with it:
std::string s;
std::getline(cin, s);
sscanf(s.c_str(), "%i%i%c", ...);
The easiest way to output a string is with:
s = "string...";
cout << s;
But printf will work too:
[fixed printf]
printf("%s", s.c_str());
The method c_str() returns a pointer to a null-terminated ASCII string, which can be used by all standard C functions.