I have written code for performing calculations. There is a loop in the code. Each loop corresponds to a different time.
For each loop, I want to write a string to an external file. The string should contain “filename_” and the number of the loop–for example:
‘fileName_4’
The problem is that it is appearing as (with the closing ‘ on the line below):
‘fileName_4
‘
If anyone could help, I would be very grateful. Here is what I have tried:
std::string convertedToString;
std::stringstream numberConverted;
storeNumberForConversion << time << endl; // time is a number, like the 4 in the example above
convertedToString = numberConverted.str() += "'";
fileNameHighestTimeStream.open ("fileName.txt", ios::out | ios::app );
fileNameHighestTimeStream << "'fileName_" << convertedToString << endl;
fileNameHighestTimeStream.close();
I have also tried:
storeNumberForConversion << time << endl; // time is a number, like the 4 in the example above
convertedToString = numberConverted.str();
fileNameHighestTimeStream.open ("fileName.txt", ios::out | ios::app );
fileNameHighestTimeStream << "'fileName_" << convertedToString << "'" << endl;
fileNameHighestTimeStream.close();
Presumably this is a typo, and you have:
You streamed
endltonumberConverted, so it contains a newline. Simple!From your comment elsewhere:
<< endlis the same as<< '\n' << flush. So you can still do the flushing yourself with<< flush, but actually usually you should just leave this up to the stream object to handle in its own time.Also the use of
+=is suspect (though it happens to yield the correct result in this case).Fixed: