I’m encountering a puzzling bug involving stringstream. I’ve got a object whose properties I want to dump to a file using dumpState(). This object has a number of member objects, each of which has had operator<< defined for them. Here’s the code:
void dumpState(int step){
stringstream s;
s << DUMP_PATH << step;
string filename;
//s >> filename;
fstream f;
f.open("fuuuuu.csv", fstream::out);
//f.open(filename.c_str(), fstream::out);
f << numNodes << '\n';
f << nodes << '\n';
f << numEdges << '\n';
f << edges << '\n';
f.close();
}
My intention is of course to write to a file whose name is determined by step. Unfortunately, I find that the values outputted are bogus. Tracking the bug down, I found that if I comment out “s>>filename;” the values are correct.
There must be some sort of flushing problem going on, but I don’t know how to fix it. Any ideas on this rather evil looking bug?
UPDATE:
I think the problem was a rather complicated error due to a mistake elsewhere in my code. After restructuring my code, the original code I posted works fine.
you should use an
ostringstreamand calls.str()to get the contents.