Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top