istream_iterator read text file.
How do you get an istream_iterator to read an entire text file word by word? Apparently its' continuously reading the first word in the sentence.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile ("Text.txt");
vector<string> store;
istream_iterator<string> in_iter(inFile) ;
istream_iterator<string> eof;
inFile.open("Text.txt");
while (in_iter != eof)
{
store.push_back(*in_iter++);
}
inFile.close();
for (auto x : store)
{
cout << x << endl;
}
return 0;
}
|
Remove line 17 (inFile.open("Text.txt");) and it will work as expected.
You could also construct your vector directly: vector<string> store(in_iter, eof);
That was simpler than expected =p. Thanks Cubbi for that and the tip.
Topic archived. No new replies allowed.