首页 > 解决方案 > 在 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 来自哪里?

标签: c++

解决方案


我已经复制了您的代码并直接运行到终端,但我得到了正确的输出。我没有得到重复的 50。


推荐阅读