我有以下的功能,即写入vector
到CSV文件:删除尾随逗号使用复制和ostream_iterator矢量书面
#include <math.h>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <iterator>
using namespace std;
bool save_vector(vector<double>* pdata, size_t length,
const string& file_path)
{
ofstream os(file_path.c_str(), ios::binary | ios::out);
if (!os.is_open())
{
cout << "Failure!" << endl;
return false;
}
os.precision(11);
copy(pdata->begin(), pdata->end(), ostream_iterator<double>(os, ","));
os.close();
return true;
}
然而,CSV文件的结尾是这样的:
1.2000414752e-08,1.1040914566e-08,1.0158131779e-08,9.3459324063e-09,
也就是说,一个尾随逗号被写入文件。当我尝试使用其他软件程序加载文件时,这会导致错误。
什么是最简单,最有效的方法来摆脱(理想情况下,永远不会写)这个尾随逗号?
您的问题将很快得到解决:http://en.cppreference.com/w/cpp/experimental/ostream_joiner – vsoftco