为什么 getline 不移动到下一行?
Why is getline not moving to next line?
所以我不明白为什么我的 getline 循环在处理完第一行后不会移动到下一行。此代码的 objective 是获取如下所示的文本文件:
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
逐行阅读,将(由 space 分隔)的元素复制到单独的变量中以备将来使用。为了检查标记化,我将变量逐行输出到 output.txt 文件中,以确保代码正常工作。这是代码:
#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;
}
理想情况下,输出文件将显示输入文件 3 行中的每个元素,每行一个元素,停止一次。good returns false(到达 eof)。结果我很好地分离了元素,但是查看输出文件显示第一行被一遍又一遍地重复,每行一个元素(就像它应该的那样)。好像getline卡在了输入文件的第一行,无法前进。知道发生了什么事吗?谢谢!
int i = 0;
while (i < 12)
{
outputFile << funcType << endl;
outputFile << lat << endl;
// ...
}
这是一个无限循环,因为您永远不会更改 i
的值。所以 getline
永远不会读取第二行,因为 getline
永远不会被第二次调用 - 代码会在发生之前陷入无限循环。
PS:即使你把它固定为迭代12次,就像你想的那样,我仍然想知道打印12次相同信息的目的是什么。
所以我不明白为什么我的 getline 循环在处理完第一行后不会移动到下一行。此代码的 objective 是获取如下所示的文本文件:
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
逐行阅读,将(由 space 分隔)的元素复制到单独的变量中以备将来使用。为了检查标记化,我将变量逐行输出到 output.txt 文件中,以确保代码正常工作。这是代码:
#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;
}
理想情况下,输出文件将显示输入文件 3 行中的每个元素,每行一个元素,停止一次。good returns false(到达 eof)。结果我很好地分离了元素,但是查看输出文件显示第一行被一遍又一遍地重复,每行一个元素(就像它应该的那样)。好像getline卡在了输入文件的第一行,无法前进。知道发生了什么事吗?谢谢!
int i = 0;
while (i < 12)
{
outputFile << funcType << endl;
outputFile << lat << endl;
// ...
}
这是一个无限循环,因为您永远不会更改 i
的值。所以 getline
永远不会读取第二行,因为 getline
永远不会被第二次调用 - 代码会在发生之前陷入无限循环。
PS:即使你把它固定为迭代12次,就像你想的那样,我仍然想知道打印12次相同信息的目的是什么。