我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
I'm using a nested while loop to Parse a text file with multiple lines but for some reason, it only goes through the first line and I don't know why
我使用嵌套的while循环来分析一个有多行的文本文件,但由于某种原因,它只经过第一行,我不知道为什么。
string file;
string line;
cout << "Whats the file name?" << endl;
cin >> file;
string inputStr;
string buf; // Have a buffer string
stringstream s; // create the string stream
int field = 0;
string input; //string for the input (i.e. name, ID, etc.)
ifstream InFile(file.c_str());
if (InFile.is_open()) {
cout << "File found" << endl;
while (getline(InFile, line)) {
cout << line << endl;
inputStr = line;
s << inputStr; //put the line into the stream
while (getline(s, input, ' ')) { //gets a string from the stream up the next comma
field++;
cout << " field" << field << " is " << input << endl;
}
cout << "DONE" << endl;
}
- 在需要使用变量时声明它们
- 你说
while (getline(s, input, ' ')) { //gets a string from the stream up the next comma
,但你给出了第三个自变量' '
(一个空格(。我想这应该是个逗号吧
固定代码:
cout << "File found" << endl;
while (getline(InFile, line)) {
cout << line << endl;
stringstream s(line); // create the string stream and init with line
while (getline(s, input, ',')) { //gets a string from the stream up the next comma
field++;
cout << " field" << field << " is " << input << endl;
}
cout << "DONE" << endl;
}
}
相关文章:
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 我不知道导致错误的原因 (C3074)
- 我有一个应该打印数组的函数,但由于某种原因它不起作用
- 在矩阵逆变器上工作,由于某种原因,我的指数循环不起作用
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- 由于某种原因,我无法创建 GLFW 上下文
- 由于某种原因,多维数组不会从函数中更改元素
- "total += i;"由于某种原因不起作用
- C++ 由于某种原因,我无法让模板在链表中工作,我敢打赌这是一个简单的错误,但我就是不明白
- 我是初学者,由于某种原因,这不起作用,我正在使用视觉工作室
- C++:当我输入一个字符串时,由于某种原因,程序期望更多,并导致整个代码出现问题
- XNextEvent 由于某种原因不起作用
- while循环中断,我不知道原因
- 由于某种原因,使用 OR 运算符执行操作不起作用
- 为什么 dev C 不运行此脚本?由于某种原因不喜欢我的矢量声明
- 我不知道我的变量是否未初始化或其他原因
- 结构由于某种原因不能识别成员
- 当我读取文件时,由于某种原因,它自己改变了文件中的位置
- 由于某种原因,设置函数不起作用
- 由于某种原因,我的C++ glm::vec3 数组似乎重置为空