c ++跳过csv文件的第一行
c++ Skip first line of csv file
我让我的程序从 .csv 文件中读取数据并输出数据,但我不希望它输出第一行。我试过使用 getline(data, line);
和 stream.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
。虽然它确实跳过了第一行,但最后两行打印了两次并混淆了。
string ID;
string sentenceIn;
string servedIn;
int sentence;
int served;
string lastName;
string firstName;
vector<string> idNum;
vector<string> sentenceLen;
vector<string> servedTime;
vector<string> lastNameIn;
vector<string> firstNameIn;
ifstream data("prisoner_data.csv");
if (data.is_open())
{
cout << "File opened successfully." << endl << endl;
while (data.good()) // !someStream.eof()
{
getline(data, ID, ',');
cout << ID << " ";
idNum.push_back(ID);
getline(data, sentenceIn, ',');
cout << sentenceIn << " ";
sentenceLen.push_back(sentenceIn);
istringstream(sentenceIn) >> sentence;
getline(data, servedIn, ',');
cout << servedIn << " ";
servedTime.push_back(servedIn);
istringstream(servedIn) >> served;
getline(data, lastName, ',');
lastNameIn.push_back(lastName);
cout << lastName << " ";
getline(data, firstName, ',');
firstNameIn.push_back(firstName);
cout << firstName << " ";
}
}
如何才能跳过第一行而不弄乱最后一行?
while (data.good())
有腥味。你最终 "eating" 多了一行。参见例如Why is iostream::eof inside a loop condition considered wrong? 了解更多详情。您通常必须直接在 while
中测试 getline
的结果,例如
while(getline(data, line)){...}
一个可能的解决方案是使用 while(getline(data, line)){...}
逐行读取文件,然后使用 stringstream(line)
并且对于每一行,再次使用 getline
解析它,现在用 [= 分隔18=]。要跳过第一行,只需先执行 getline(data, line);
,然后再执行 while(getdata(data, line)){ /* process line */}
。下面是一个简单的例子:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cstdlib>
int main()
{
std::ifstream data("prisoner_data.csv");
if (!data.is_open())
{
std::exit(EXIT_FAILURE);
}
std::string str;
std::getline(data, str); // skip the first line
while (std::getline(data, str))
{
std::istringstream iss(str);
std::string token;
while (std::getline(iss, token, ','))
{
// process each token
std::cout << token << " ";
}
std::cout << std::endl;
}
}
我让我的程序从 .csv 文件中读取数据并输出数据,但我不希望它输出第一行。我试过使用 getline(data, line);
和 stream.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
。虽然它确实跳过了第一行,但最后两行打印了两次并混淆了。
string ID;
string sentenceIn;
string servedIn;
int sentence;
int served;
string lastName;
string firstName;
vector<string> idNum;
vector<string> sentenceLen;
vector<string> servedTime;
vector<string> lastNameIn;
vector<string> firstNameIn;
ifstream data("prisoner_data.csv");
if (data.is_open())
{
cout << "File opened successfully." << endl << endl;
while (data.good()) // !someStream.eof()
{
getline(data, ID, ',');
cout << ID << " ";
idNum.push_back(ID);
getline(data, sentenceIn, ',');
cout << sentenceIn << " ";
sentenceLen.push_back(sentenceIn);
istringstream(sentenceIn) >> sentence;
getline(data, servedIn, ',');
cout << servedIn << " ";
servedTime.push_back(servedIn);
istringstream(servedIn) >> served;
getline(data, lastName, ',');
lastNameIn.push_back(lastName);
cout << lastName << " ";
getline(data, firstName, ',');
firstNameIn.push_back(firstName);
cout << firstName << " ";
}
}
如何才能跳过第一行而不弄乱最后一行?
while (data.good())
有腥味。你最终 "eating" 多了一行。参见例如Why is iostream::eof inside a loop condition considered wrong? 了解更多详情。您通常必须直接在 while
中测试 getline
的结果,例如
while(getline(data, line)){...}
一个可能的解决方案是使用 while(getline(data, line)){...}
逐行读取文件,然后使用 stringstream(line)
并且对于每一行,再次使用 getline
解析它,现在用 [= 分隔18=]。要跳过第一行,只需先执行 getline(data, line);
,然后再执行 while(getdata(data, line)){ /* process line */}
。下面是一个简单的例子:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cstdlib>
int main()
{
std::ifstream data("prisoner_data.csv");
if (!data.is_open())
{
std::exit(EXIT_FAILURE);
}
std::string str;
std::getline(data, str); // skip the first line
while (std::getline(data, str))
{
std::istringstream iss(str);
std::string token;
while (std::getline(iss, token, ','))
{
// process each token
std::cout << token << " ";
}
std::cout << std::endl;
}
}