简体   繁体   English

Getline从txt文件读取数据

[英]Getline to read data from txt file

I'm having a bit of a problem with extracting data from a simple .txt file with the getline command. 使用getline命令从简单的.txt文件提取数据时,我遇到了一些问题。

The txt file is very simple: a column of 400 numbers. txt文件非常简单:一列400个数字。 I use a vector to store them with the following code: 我使用向量将它们存储为以下代码:

int i = 0;
string line;
vector <double> vec;

while (getline(input, line))
{
    vec.push_back(i);
    N++;
    input >> vec[i];
    i++;
} 

It correctly creates a vector of 400 elements but first line of txt file is ignored (I end up with vec[0] = 2nd line of txt file instead of 1st) and 399th element is 399 instead of the 400th line of txt file. 它正确地创建了一个包含400个元素的向量,但是txt文件的第一行被忽略了(我以vec [0] = txt文件的第二行而不是第一行结束),而第399个元素是399,而不是txt文件的第400行。

I tried several other ways to extract this data but it was unsuccessful. 我尝试了其他几种方法来提取此数据,但未成功。

Thank you for your help! 谢谢您的帮助!

EDIT: 编辑:

I have edited the code according to some of the remarks: 我已经根据一些评论编辑了代码:

vector <double> vec;
string line;
double num;

while (getline(input, line))
{
    input >> num;
    vec.push_back(num);
}

Unfortunately, it still skips the first line of my text file. 不幸的是,它仍然跳过了我的文本文件的第一行。

EDIT 2 --> SOLUTION: 编辑2->解决方案:

Thanks to all of your remarks, I realized that I was doing something wrong when using both getline and input >> num; 感谢您的所有评论,我意识到同时使用getline和input >> num时我做错了什么;

Here is how the problem was solved: 解决问题的方法如下:

double num;
vector <double> vec;

while (input >> num)
{
    vec.push_back(num);
}

You can read the entire file into a vector just by passing std::istream_iterator to std::vector constructor, without loops: 您只需将std::istream_iterator传递给std::vector构造函数即可,无需循环即可将整个文件读入向量:

std::vector<int> v{
    std::istream_iterator<int>{input}, 
    std::istream_iterator<int>{}
};

Eg: 例如:

#include <iostream>
#include <iterator>
#include <vector>
#include <exception>

template<class T>
std::vector<T> parse_words_into_vector(std::istream& s) {
    std::vector<T> result{
        std::istream_iterator<T>{s},
        std::istream_iterator<T>{}
    };
    if(!s.eof())
        throw std::runtime_error("Failed to parse the entire file.");
    return result;
}

int main() {
    auto v = parse_words_into_vector<int>(std::cin);
    std::cout << v.size() << '\n';
}

You loose the first line due to reading from the file once more - here: 由于再次读取文件,因此您松散了第一行-此处:

while (getline(input, line))
    // ^^^^^^^ Here you read the first line
{
    input >> num;
 // ^^^^^^^^ Here you will read the second line

You told you want a vector of doubles - like: 您告诉过您想要双打的向量-例如:

std::vector<double> vec;

So you should use std::stod to convert the line read by getline into a double. 因此,您应该使用std::stodgetline读取的行转换为double。 Like: 喜欢:

while (std::getline(input, line))
{
    // Convert the text line (i.e. string) to a floating point number (i.e. double)
    double tmp;
    try
    {
        tmp = stod(line);
    }
    catch(std::invalid_argument)
    {
        // Illegal input
        break;
    }
    catch(std::out_of_range)
    {
        // Illegal input
        break;
    }

    vec.push_back(tmp);
} 

Don't do input >> num; 不要 input >> num; inside the loop. 在循环内。

If you really want to use input >> num; 如果您真的想使用input >> num; then you shall not use getline . 那么您将不能使用getline That is - you can use either but not both. 也就是说-您可以使用任何一个,但不能同时使用。

Change your while loop like below:- 如下更改您的while循环:

while (getline(input, line))
{
    vec.push_back(line);
    //N++;
    //input >> vec[i];
    //i++;
} 

Also try with below option 也可以尝试以下选项

 do{
    vec.push_back(i);
    //N++;
    //i++;
 }while (input >> vec[i++]);

You are starting by putting 0 in vector in first iteration: 首先在第一次迭代中将vector放入0:

vec.push_back(i);

then after you read first line you read next string, but stream get pointer from file is already in different place, so you override this 0 and skip first value from stream. 然后,在读取第一行之后,您将读取下一个字符串,但是从文件中获取流的指针已经在不同的位置,因此您覆盖此0并从流中跳过第一个值。 What is worse that is oddly converted to double: 更糟糕的是,它奇怪地转换为两倍:

input >> vec[i];

This way you will get wrong. 这样,您会出错。 Try this: 尝试这个:

while (std::getline(file, line)) {
    //In c++11
    vec.emplace_back(std::stod(line));
    //In c++ 98, #include <stdlib.h> needed
    //vec.push_back(atof(line.c_str())); 
}

This assumes you will always have proper file. 这假设您将始终拥有正确的文件。

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

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