Best way to the last byte from a file with fstream?

浪子不回头ぞ 提交于 2021-01-29 01:33:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!