我使用以下代码片段将值写入 .txt 文件:
fstream f1;
f1.open("output.txt", ios::out);
{
for (const auto& avg : clusAvg)
{
f1 << avg << ",";
}
}
f1.close();
这会生成一个如下所示的列表:20,30,40,50,
我的问题是,如何消除列表中的最后一个逗号?
最佳答案
尝试这样做
fstream f1;
f1.open("output.txt", ios::out);
{
bool first = true;
for (const auto& avg : clusAvg)
{
if(!first) f1 << ", ";
first = false;
f1 << avg;
}
}
f1.close();
关于c++ - 写入文件时如何删除最后一个逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62195035/