file io - Is there a portable way to redirect stdin to string in C++? -
is possible in c++ redirect stdin string in c++?
using freopen can redirect stdin file, both scanf , cin use content of file. using manipulations stringstream , cin.rdbuf() can redirect cin string, call cin work string. scanf continue work previous input stream. guess possible unix's pipes it's not available under windows.
is possible solve in portable way?
this isn't best way it, can make work this:
// warning - has side effects (sets noskipws) don't care (its example) ostream& operator<< (ostream& out, istream& in) { in >> noskipws; char c; in >> c; while (in) { out << c; in >> c; } return out; } int main() { ostringstream inputstr; inputstr << cin; inputstr.str(); // contains data stdin return; }
Comments
Post a Comment