Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;从文件中读取跳过某些行_C++_File_Csv - Fatal编程技术网

C++ c++;从文件中读取跳过某些行

C++ c++;从文件中读取跳过某些行,c++,file,csv,C++,File,Csv,我有这种格式的文本文件 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)) {

我有这种格式的文本文件

 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;
    }
}
字符串csvLine;
int用户输入;
ifstream数据(“CWoutput.txt”);
弦线;
字符串str;
向量表;
while(getline(数据,行))
{
线状流线状流(线状);
字符串单元;
while(getline(lineStream,单元格'\n'))
{
if(单元格位于(0)!='w'| |单元格位于(0)!='P')
{ 
while(getline(行流,单元格','))
{

cout在此语句中
while(getline(lineStream,cell,'\n'))

您的
lineStream
不包含
'\n'
字符,因为它被上一个
getline(data,line)
函数丢弃

您的while循环可以简化为:

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;
   }
}
while(getline(数据,行))
{
data.clear();
如果(第[0]行!='w'和第[0]行!='P'){
线状流线状流(线状);
字符串单元;
while(getline(行流,单元格','))
{

cout为什么在循环中使用
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());
        }
}
while(getline(数据,行))
{
如果(第(0)行!=“w”|第(0)行!=“P”)
{ 
标准::向量x=分割(线,,);
insert(listofscore.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;
}
std::vector&split(常量std::string&s、char-delim、std::vector&elems){
标准::stringstream ss(s);
std::字符串项;
while(std::getline(ss,item,delim)){
元素推回(项目);
}
返回元素;
}
std::vector split(const std::string&s,char delim){
std::向量元素;
拆分(s、delim、elems);
返回元素;
}

除此之外,请将
if(hasWon==true){return true;}else{return false;}
替换为
return hasWon;
。是的,或者直接
return this->validateRule2(vectorScores)
或者
return this->validateRule2(this->winner(listOfCells))
它不会转到下一行