I would like to ask why does the last word in the input file will appear twice in the standard output? Below is my code. Please help. Thank you.
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;
void fileinput(vector<string> & v)
{
ifstream input("afile.txt");
string word;
if(!input)
cout << "cannot read from files";
while(!input.eof()){
input>>word;
v.push_back(word);
}
input.close();
}
int main()
{
vector<string> v0;
fileinput(v0);
auto b=v0.cbegin();
auto e=v0.cend();
for (auto it=b; it!=e;++it)
cout<<*it<<endl;
return 0;
}
Last edited on