为什么在同一个文本文件上多次运行的程序会有不同的输出
Why is there different outputs from a program ran multiple times on the same text file?
所以我有一个程序可以读取一个文本文件,该文件的行中有用逗号分隔的数字。我获取文本文件的每一行,并逐个字符地对其进行解析。如果我说到逗号,我就继续。当我得到不同于逗号(应该是整数)的东西时,我会将该字符转换为整数并打印它。我的程序无法正常工作,有时只打印两行空行,有时会打印"1 1 2 2 3 4",然后再打印一行空行。
程序:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
ifstream infile(argv[1]);
string str;
int num, i;
while (!infile.eof()) {
getline(infile, str);
if (str.length() == 0) continue;
else {
for (i == 0; i < str.length(); ++i) {
if (str[i] == ',') continue;
else {
num = str[i] - '0';
cout << num << " ";
}
}
}
cout << endl;
}
infile.close();
return 0;
}
文本文件:
1,1,1,2,2,3,3,4,4
2,3,4,5,5
您在for循环中有一个拼写错误,应该是i = 0
,而不是==
相关文章:
- 如何从Windows应用程序输出到标准?
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 而循环:简单的除法程序输出零,不明白为什么
- Qt creator 4.11,在应用程序输出面板中创建一个链接
- 使用复制构造函数的程序输出错误
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- 为什么在 Direct X 11 中没有我的程序输出?
- 我怎样才能阻止我的程序输出时间,它使我的程序难以阅读
- 程序输出奇怪的字符
- 程序输出"nan"
- 为什么我的C++程序输出不同的数字来将米转换为英尺?
- 3D 纹理大小会影响程序输出,而不会引发错误
- C++,程序输出无效输入,但继续通过循环
- 如何连接C++和Excel(从C++程序输出到Excel电子表格)
- 为什么我的C 程序输出乱码代码
- QT小部件应用程序输出到控制台
- 混淆了具有虚拟继承的程序输出
- 为什么我的C++程序输出这个
- 为什么我的程序输出不断变化?EAX寄存器不断更改值