简体   繁体   English

为什么getline不能移到下一行?

[英]Why is getline not moving to next line?

So I can't figure out why my getline loop will not move to the next line after processing the first. 所以我不知道为什么我的getline循环在处理完第一行后不会移到下一行。 The objective of this code is to take a text file that looks something like this: 这段代码的目的是获取一个看起来像这样的文本文件:

a 22.55736 110.9237 10.0 9.0 8.0 -1.0 1.0 -1.0 5.0 6.0 11.0
b 40.5567 50.556 10.0 9.0 8.0 -1.0 1.0 -1.0 5.0 6.0 11.0
c 26.7798223 24.5 10.0 9.0 8.0 -1.0 1.0 -1.0 5.0 6.0 11.0

Read the lines one by one, copying the (delimited by a space) elements into individual variables for future use. 逐行阅读各行,将元素(由空格分隔)复制到各个变量中,以备将来使用。 To check the tokenizing, I have the variables being output into an output.txt file line by line to ensure the code is working. 为了检查标记化,我逐行将变量输出到output.txt文件中,以确保代码正常工作。 Here's the code: 这是代码:

#include "stdafx.h""
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main() {

    string stringLine;
    ifstream inFile;
    inFile.open("missionData.txt");
    ofstream outputFile;
    outputFile.open("outputFile.txt");

    if (inFile.good())
    {
        while (getline(inFile, stringLine))
        {

            //getline(inFile, stringLine);  //Read line from txt into stringLine
            istringstream iss(stringLine);
            vector<string> lineArray{ istream_iterator<string>{iss},
                                            istream_iterator<string>{} };

            //Convert tokenized vector into individual, usable parameters
            const char* funcType = lineArray.at(0).c_str();
            double lat = atof(lineArray.at(1).c_str());
            double lon = atof(lineArray.at(2).c_str());
            float alt = stof(lineArray.at(3));
            float speed = stof(lineArray.at(4));
            float radius = stof(lineArray.at(5));
            float yaw = stof(lineArray.at(6));
            float duration = stof(lineArray.at(7));
            float imgOn = stof(lineArray.at(8));
            float vidOn = stof(lineArray.at(9));
            float micOn = stof(lineArray.at(10));
            float sensOn = stof(lineArray.at(11));

            int i = 0;
            while (i < 12)
            {
                outputFile << funcType << endl;
                outputFile << lat << endl;
                outputFile << lon << endl;
                outputFile << alt << endl;
                outputFile << speed << endl;
                outputFile << radius << endl;
                outputFile << yaw << endl;
                outputFile << duration << endl;
                outputFile << imgOn << endl;
                outputFile << vidOn << endl;
                outputFile << micOn << endl;
                outputFile << sensOn << endl;
            }
        }
        inFile.close();
    }
    return 0;
}

Ideally, the output file will display each element from the 3 lines of the input file, one element per line, stopping once .good returns false (reach eof). 理想情况下,输出文件将显示输入文件的3行中的每个元素,每行显示一个元素,一旦.good返回false(到达eof)就停止。 Turns out I separate the elements just fine, but looking at the output file shows the first line being repeated over and over, one element per line(like it should be). 事实证明,我很好地分离了元素,但是查看输出文件会发现第一行重复出现,每行重复一个元素(应该是这样)。 Its as if getline is stuck on the first line of the input file and cannot move forward. 就像getline停留在输入文件的第一行一样,无法前进。 Any idea what's going on? 知道发生了什么吗? Thanks! 谢谢!

int i = 0;
while (i < 12)
{
    outputFile << funcType << endl;
    outputFile << lat << endl;
    // ...
}

This is an infinite loop because you never change the value of i . 这是一个无限循环,因为您永远不会更改i的值。 So getline never reads a second line because getline is never called a second time - the code gets stuck in the infinite loop before that can happen. 因此, getline永远不会读取第二行,因为getline不会被第二次调用-在此之前,代码陷入了无限循环。

PS: Even if you fix it to iterate 12 times, like you presumably intended, I still wonder what the purpose of printing the same information twelve times is. PS:即使您将其固定为迭代12次(如您预期的那样),我仍然想知道打印十二次相同信息的目的是什么。

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

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