이 코드를 추출하여 CSV 파일을 구문 분석했지만 첫 번째 n-1 행의 첫 번째 요소는 읽지 않습니다. 그 이유는 모르겠지만 데이터를 새로운 빈 파일에 복사하고 CSV 파일로 저장하면 오류가 사라지고 제대로 작동합니다. original (오류 발생) 및 copied (오류가 발생하지 않음) CSV 파일에 대한 링크는 다음과 같습니다. 왜 이런 일이 일어나는 지 도와 주실 수 있겠습니까?C++의 CSV 파서가 첫 번째 요소를 읽지 않습니다
감사합니다. 당신의 orginal 한 파일에
#include <boost/tokenizer.hpp>
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <cstdlib>
int main(int argc, char** argv)
{
using namespace std;
if (argc != 2)
{
cerr << "Usage: " << argv[0] << " <csv file>" << endl;
return -1;
}
vector< vector<string> > csv_values;
fstream file(argv[1], ios::in);
if (file)
{
typedef boost::tokenizer< boost::char_separator<char> > Tokenizer;
boost::char_separator<char> sep(",");
string line;
while (getline(file, line))
{
Tokenizer info(line, sep); // tokenize the line of data
vector<string> values;
for (Tokenizer::iterator it = info.begin(); it != info.end(); ++it)
{
// convert data into double value, and store
values.push_back(it->c_str());
}
// store array of values
csv_values.push_back(values);
}
}
else
{
cerr << "Error: Unable to open file " << argv[1] << endl;
return -1;
}
// display results
cout.precision(1);
cout.setf(ios::fixed,ios::floatfield);
for (vector< vector<string> >::const_iterator it = csv_values.begin(); it != csv_values.end(); ++it)
{
const vector<string>& values = *it;
for (vector<string>::const_iterator it2 = values.begin(); it2 != values.end(); ++it2)
{
cout << *it2 << " ";
}
cout << endl;
}
}
복사 된 파일의 끝에 빈 줄만 차이가 있습니다. – KIIV