2014-11-23 1 views
0
readInputRecord(ifstream &inputFile, 
string &taxID, string &firstName, string &lastName, string &phoneNumber) {  
    while (!inputFile.eof()) { 
     inputFile >> firstName >> lastName >> phoneNumber >> taxID;  
    } 
} 

여러분도 알다시피 나는 표준 읽기 입력 파일과 같은 데이터를 읽습니다. 문제는 데이터 필드가 ","와 같이 비어있을 수 있으며 괄호 사이에 데이터가 없음을 나타냅니다. 나는 여기와 다른 곳에서 포럼을 읽었으며 일반적인 방법은 getline (stuff, stuff, ',')을 사용하는 것 같다.하지만 쉼표로 멈추는 데이터를 읽는다. 변수가 읽혀지면 출력 파일이 읽은 다음 ","을 출력해야하기 때문에 쉼표를 포함하는 방법은 무엇입니까?쉼표를 포함하여 1, 2, 3 같은 입력 파일에서 쉼표로 구분 된 데이터를 읽고 싶다면?

+0

와 함께 올바른 솔루션을 제공되고있다 확실하게 읽을 필요가 없다? – Oncaphillis

+0

예. 입력 예 : john, doe, 123-456-7890,123-45-6789 –

+0

http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong –

답변

0

당신이있는 경우 부스트-DEV 설치, 다음 헤더 포함 파일 <boost/algorithm/string.hpp>

void readInputRecord(std::ifstream &inputFile, std::vector<std::string>& fields) { 
    std::string line; 
    fields.clear(); 
    while (std::getline(inputFile, line)) { 
      boost::split(fields, line, boost::is_any_of(",")); 
      for (std::vector<std::string>::iterator it = fields.begin(); it != fields.end(); ++it) 
       std::cout << *it << "#"; 

      std::cout << std::endl; 
    } 
} 

모든 필드가 벡터에 포함는 빈 필드를 포함한다. 코드는 테스트되지 않았지만 작동해야합니다.

0

명시 적으로는 ','A ','와 std::getline(...)는 데이터 라인으로 구성되어 std::stringstream

// Read the file line by line using the 
// std line terminator '\n'  

while(std::getline(fi,line)) { 
    std::stringstream ss(line);      
    std::string cell;        

    // Read cells withing the line by line using 
    // ',' as "line terminator"   
    while(std::getline(fi,cell,',')) { 
     // here you have a string that may be '' when you got 
     // a ',,' sequence 
     std::cerr << "[" << cell << "]" << std::endl; 
    } 
} 
관련 문제