2016-08-17 10 views
0

이 코드를 추출하여 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; 
    } 
} 
+0

복사 된 파일의 끝에 빈 줄만 차이가 있습니다. – KIIV

답변

0

새 라인은 라인에 마지막 varible와 코드에 의해 읽은 후 인쇄 된 carriage return,로 끝납니다. 그래서 첫 번째 줄은 다음과 같이 인쇄됩니다.

1 2 3 4 5\r 

그런 다음 줄의 시작 부분에 "1"을 인쇄합니다.

당신은 쉽게 디버거에서 볼 수 있습니다 :)

+0

감사합니다. 이것은 캐리지 리턴에 관한 소식을 처음 들었을 때입니다. :) – vlavyb