简体   繁体   中英

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. 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. 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). 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. 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 . 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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