c++ - 在 C++ 中从输入文件中读取
问题描述
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream infile;
int age;
infile.open("intestfile.txt");
if(infile){
while(!infile.eof()) {
infile >> age;
cout << age << endl;
}
} else {
cout << "Error opening file." << endl;
}
infile.close();
}
我期待像这样的输出;10 20 30 40 50(此列表垂直显示)。为什么我得到 10 20 30 40 50 50 而输入文件只有 10 20 30 40 50。重复的 50 来自哪里?
解决方案
我已经复制了您的代码并直接运行到终端,但我得到了正确的输出。我没有得到重复的 50。
推荐阅读
- sql - 防止在 AWS Glue 中多次处理文件
- windows - 使用批处理脚本在多个文件上搜索文本,如果在其中找到文本,则重命名文件
- python - Pyinfra 缺少位置参数
- android - 以编程方式为 AnimatedVectorDrawable 和 Compat 设置持续时间和 startOffset
- javascript - 我不断收到 TypeError: Cannot read property 'find' of undefined
- python - 有没有办法使用模式匹配回调删除(修改)绘图破折号中的父元素?
- java - 与继承类不兼容的类型
- unity3d - 2个统一的维度滑块
- reactjs - 谷歌身份没有在反应中调用回调
- javascript - 选择下拉项时更改父元素的背景?