简体   繁体   English

我应该如何读入一个文本文件,其中每一行都包含C ++中的各种数据类型?

[英]How should I go about reading in a text file with each line containing various data types in C++?

I have a textfile with lots of data. 我有一个包含大量数据的文本文件。 Each row has two integers which specify coordinates, followed by the name of the coordinate, and additional attributes. 每行都有两个指定坐标的整数,其后是坐标名称和其他属性。

I'm trying to read in all the attributes into a vector and then later access the different attributes and each row. 我试图将所有属性读入向量,然后稍后访问不同的属性和每一行。

I'm using getline to read in each line, but how would I be able to access the various attributes instead of the whole line? 我正在使用getline读取每一行,但是我如何能够访问各种属性而不是整个行?

Here is my code: 这是我的代码:

    while (getline(location_file, line)) {
          vector<string> file;
          file.push_back(line);
}

Would structs be a better option? 结构会是更好的选择吗?

Instead of using getline to read the whole line, since the input seems to be standard, you can use a std::istream and the >> operator to read formatted input that ignores whitespaces. 由于输入似乎是标准的,因此可以使用std::istream>>运算符读取忽略空格的格式化输入,而不是使用getline读取整行。 You basically have to read 2 integers and then use getline to read the rest of the line. 基本上,您必须读取2个整数,然后使用getline读取其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM