2013-10-25 2 views
0

У меня есть текстовый файл в этом форматеC++ чтение из файла пропустить определенные строки

wins = 2 
Player 
10,45,23 
90,2,23 

я должен хранить 10 45 23 в вектор и передать его функции, это проблема распадается после первой строки

string csvLine; 
int userInput; 
ifstream data("CWoutput.txt"); 
string line; 
string str; 
vector<string> listOfScores; 
while(getline(data,line)) 
{ 
    stringstream lineStream(line); 
    string  cell; 
    while(getline(lineStream,cell,'\n')) 
    { 
     if (cell.at(0) != 'w' || cell.at(0) != 'P') 
     { 
      while(getline(lineStream,cell,',')) 
      { 
       cout<<line<<endl; 

       listOfScores.push_back(cell); 
      } 
     } 
    } 
    vector<transaction> vectorScores= this->winner(listOfCells); 
    bool hasWon= false; 
    hasWon= this->validateRule2(vectorScores); 
    if(hasWon== true) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
+2

Помимо всего прочего, пожалуйста, замените 'если (hasWon == TRUE) {возвращает истину; } else {return false; } 'с' return hasWon; '. – BoBTFish

+2

yes или прямо 'return this-> validateRule2 (vectorScores)' –

+1

Или 'return this-> validateRule2 (this-> winner (listOfCells))' –

ответ

0

Почему вы используете linestaream в цикле? Вы получаете целую линию, вызывая getline(data,line).

так что вы можете сделать

while(getline(data,line)) 
{ 


     if (line.at(0) != 'w' || line.at(0) != 'P') 
     { 
      std::vector<std::string> x = split(line, ','); 
      listOfScores.insert(listOfScores.end(),x.begin(),x.end()); 
     } 
} 

и вы можете использовать расщепленные функции:

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) { 
    std::stringstream ss(s); 
    std::string item; 
    while (std::getline(ss, item, delim)) { 
     elems.push_back(item); 
    } 
    return elems; 
} 


std::vector<std::string> split(const std::string &s, char delim) { 
    std::vector<std::string> elems; 
    split(s, delim, elems); 
    return elems; 
} 
0

В этом заявлении while(getline(lineStream,cell,'\n'))

Ваш lineStream не содержит '\n' характер, поскольку она отбрасывается предыдущим getline(data, line) функция.

Ваше время цикла может быть упрощена что-то вроде:

while(getline(data,line)) 
{ 
    data.clear(); 
    if (line[0] != 'w' && line[0] != 'P') { 
     stringstream lineStream(line); 
     string  cell; 
     while(getline(lineStream,cell,',')) 
     { 
     cout<<line<<endl; 

     listOfScores.push_back(cell); 
     } 
     vector<transaction> vectorScores= this->winner(listOfCells); 
     bool hasWon= false; 
     hasWon= this->validateRule2(vectorScores); 
     return hasWon; 
    } 
} 
+0

это не событие перейти к следующей строке – meWantToLearn

+0

Попробуйте очистить неудавшиеся флаги. Я обновил свой код. –

Смежные вопросы