In c++, you can use string exactly as files, using the classes defined in the sstream
header:
#include <sstream>
//...
std::string str=...; // your string
std::istrstream in(str); // an istream, just like ifstream and cin
std::string line;
while(std::getline(in,line)){
//do stuff with line
}
This is a bit simplistic, but you get the idea.
You can use in
just as you would use cin
, e.g. in>>x
etc. Hence the solutions from How do I iterate over cin line by line in C++? are relevant here too - you might want to look at them for the "real" answer (just replace cin
with your own istream
Edit:
As a side note, you can create strings in the same way you print to the screen, using the ostream
mechanism (like cout
):
std::ostringstream out;
out << header << "_" << 3.5<<".txt";
std::string filename=out.str();