C++ 的文件 I/O

时间:2021-05-13 12:57:29

标签: c++

我在输入文件时遇到问题。

#include <iostream> 
#include <vector>
#include <fstream>

using namespace std;

int main (int argc, char *argv[]) {
ifstream my_file(argv[1]);

cout << argv[1] << endl;

  int input;
  vector<int> w;

  while(!my_file.eof()) {

     my_file >> input;
     cout << "current input: " << input << endl;
     w.push_back(input);
  }
   
  return 0;

}

我的数据文件是“input1.dat”这是

2 3 4 1 2 1 3 5 4 3 

但是,我得到的输出(w)是

2 3 4 1 2 1 3 5 4 3 3 

为什么最后多了一个 3?循环应该在while循环到达文件末尾时结束。

0 个答案:

没有答案
相关问题