问题
I am generating a CSV file, and of course I get the wonderful trailing comma at the end
data1, data2, data3,
data1, data2, data3,
data1, data2, data3,
data1, data2, data3,
Can I use the fstream facilities to remove the last comma? I am more familiar with the C fopen and so I'm a bit out of water, and just looking for a quick, best-practices solution.
回答1:
The only way to do this with streams is to write your output to a temp buffer / file (e.g. std::stringstream
), and then copy all but the last comma to the final destination.
A better approach would be to make sure the last comma never appears in the first place.
来源:https://stackoverflow.com/questions/22831846/best-way-to-the-last-byte-from-a-file-with-fstream