У меня есть файл CSV, и я читаю файл следующим образом:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
typedef vector <double> record_t;
typedef vector <record_t> data_t;
data_t data;
istream& operator >> ( istream& ins, record_t& record )
{
record.clear();
string line;
getline( ins, line );
// Using a stringstream to separate the fields out of the line
stringstream ss( line );
string field;
while (getline( ss, field, ',' ))
{
// for each field we wish to convert it to a double
stringstream fs( field );
double f = 0.0; // (default value is 0.0)
fs >> f;
// add the newly-converted field to the end of the record record.push_back( f );
}
return ins;
}
//-----------------------------------------------------------------------------
istream& operator >> ( istream& ins, data_t& data )
{
data.clear();
record_t record;
while (ins >> record)
{
data.push_back( record );
}
return ins;
}
//----------------------------------------------
int main() {
ifstream infile( "2010.csv" );
infile >> data;
if (!infile.eof())
{
cout << "Error with the input file \n";
return 1;
}
infile.close();
//do something with "data"
// write the data to the output.
}
Теперь файл, который я использую, похож на
A,B,c,D,E,F
1,1,1,1,1,1,
2,2,2,2,2,2,
3,3,3,3,3,3,
Так что если заголовок нетам программа работает нормально.Как я могу удалить заголовок и вставить его обратно в выходной файл и как сохранить прежнее форматирование?
Я где-то адаптировал этот код и не помню исходный код.