用fstream从文件中获取最后一个字节的最佳方法? [英] Best way to the last byte from a file with fstream?

查看:55
本文介绍了用fstream从文件中获取最后一个字节的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成一个CSV文件,当然,最后我得到了一个奇妙的结尾逗号

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,

我可以使用fstream功能删除最后一个逗号吗?我对C fopen更加熟悉,所以我有点没水了,只是在寻找一种快速,最佳实践的解决方案.

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.

推荐答案

使用流执行此操作的唯一方法是将输出写入临时缓冲区/文件(例如 std :: stringstream ),然后将除最后一个逗号外的所有逗号都复制到最终目的地.

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.

这篇关于用fstream从文件中获取最后一个字节的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆