Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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++_File_Vector_Delimiter - Fatal编程技术网

C++ 将文件读入由逗号和新行分隔的向量

C++ 将文件读入由逗号和新行分隔的向量,c++,file,vector,delimiter,C++,File,Vector,Delimiter,假设我想读入一个.txt文件,并以这种方式格式化 Max,1979 Wade,1935 Hugh,1983 Eric,1936 下面是我用来 读入文件。 将其分别存储到名称和年份的字符串和int向量中 void calcAges(){ while (getline(infile, line, ',')){ names.push_back(line); years.push_back(line); } } void printNames(){ cout << "\n

假设我想读入一个.txt文件,并以这种方式格式化

Max,1979
Wade,1935
Hugh,1983
Eric,1936
下面是我用来

读入文件。 将其分别存储到名称和年份的字符串和int向量中

void calcAges(){
while (getline(infile, line, ',')){
    names.push_back(line);
    years.push_back(line);
}
}

void printNames(){
cout << "\n\tDisplaying data...\n";

    for (int i = 0; i < counter; i++){
        cout << (i + 1) << ".\tName: " << names[i] << "\tYear: " << years[i] << endl;
    }
}

但是,我在尝试使其成为一个逗号和一个新行的时候遇到了麻烦,所以我在内嵌中读取的文件被拆分。我将这些变量存储到一个向量数组中,以便以后可以进行排序和切换。这一点我很困惑。

一旦您将“,”作为分隔符,新行将被视为普通字符。因此,在不指定分隔符的情况下使用getline默认值为新行,并尝试从您获得的字符串中提取名称和年份。使用find_first_of和substr可以很容易地完成

例如:

while(getline(infile,str))
{
     int index = str.find_first_of(',');
     string name = str.substr(0,index);
     string date = str.substr(index+1);
      // Do something with name and date

}
在这类操作中,A的功能要强大得多。然而,在您认为相当简单的情况下,您可以对字符串进行简单的操作。 我建议将每一行读入临时字符串,然后用逗号将其拆分,并将值添加到向量中,如下所示:

void calcAges(){
    while (getline(infile, line)){  // Read a whole line (until it reads a '\n', by default)
        names.push_back(line.substr(0,line.find(",")); // Read from the beginning of the string to the comma, then push it into the vector
        years.push_back(std::stoi(line.substr(line.find(",") + 1)); // Read from the comma to the end of the string, parse it into an integer, then push it into the vector
    }
}
我假设您使用库中的std::string作为行变量的类型

顺便说一句,我没有编译和测试它,所以我不确定它是否能正常工作,但我写它只是为了让您了解逻辑方法


干杯

在加入“年”之前,再加入一个GetLineInfle,这一次没有熟食。这会读到剩下的部分,真不敢相信,我想,甚至想都不敢想!然而,当我尝试输入第二个getlineinfile,line时,我得到一个错误,说getline不能接受int?除此之外,将我的年份向量更改为字符串可以使代码正常运行。但是存储一个年的字符串向量是没有意义的。非常感谢你!当你进入年份时,你必须清楚地把线转换成整数谢谢!然而,我试图理解子字符串的第二部分。当你说索引+1时,你是说“在逗号之后,以及在逗号之后的任何东西”吗?在文本文件“\n”中看到新行后是否停止读取?str一次只包含一行。所以第二行一直读到那一行的末尾。是的,逗号之后的任何内容都被放入第二个字符串中。我明白了,谢谢。这对我很有帮助。因此,如果文本文件行是例如Max,1979,名称带有逗号和空格,我会写:string date=str.substrindex+2;?这是有效的,所以我想读取文件的最佳方式之一是逐行读取,然后从该行开始读取?我明白你对我的代码进行简单的操作是什么意思。谢谢你!这实际上取决于具体情况,但通常您将从磁盘读取文件,这意味着读取操作比代码的其余部分慢得多。因此,分批执行文件访问操作是一种代码优化实践,目的是使事情尽可能顺利。当然,我所说的优化甚至不会被注意到,除非你操作大量的数据,但我想一个好的实践总是好的。
void calcAges(){
    while (getline(infile, line)){  // Read a whole line (until it reads a '\n', by default)
        names.push_back(line.substr(0,line.find(",")); // Read from the beginning of the string to the comma, then push it into the vector
        years.push_back(std::stoi(line.substr(line.find(",") + 1)); // Read from the comma to the end of the string, parse it into an integer, then push it into the vector
    }
}