C++ 删除文件的第一行并将其插入到输出中
我有一个csv文件,我阅读该文件如下:C++ 删除文件的第一行并将其插入到输出中,c++,iostream,stringstream,C++,Iostream,Stringstream,我有一个csv文件,我阅读该文件如下: #include <fstream> #include <iostream> #include <sstream> #include <string> #include <vector> #include <algorithm> #include <numeric> using namespace std; typedef vector <double> re
#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.
}
因此,如果标题不在那里,程序工作正常。如何删除标题并将其插入到输出文件中,以及如何保持相同的格式
我从某个地方改编了这段代码,但我不记得源代码。只需对第一行使用另一个字符串,在while循环中,将第一行视为特殊情况,跳过对所有其他行的正常处理。只需对第一行和while循环使用另一个字符串,将第一行视为特殊情况,跳过所有其他行的正常处理。如何先读取第一行,然后将流缓冲区放入此函数? 似乎您不想更改函数
ifstream infile( "2010.csv" );
string header;
std::getline(infile, header);
infile >> data;
先读取第一行,然后将流缓冲区放入这个函数中,怎么样? 似乎您不想更改函数
ifstream infile( "2010.csv" );
string header;
std::getline(infile, header);
infile >> data;