0

I have .txt file with contents apple microsoft IBM Bell AT&T, and I want to read them into a vector<string> in C++. Here is the code I tried:

    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <iterator>
    #include <string>
    using namespace std;

    int main()
    {
        ifstream data_file("/Users/iAPPLE/Desktop/data.txt");
        istream_iterator<string> start(data_file), end;
        vector<string> data(start, end);
        for (auto it = start; it != end; it++) cout << *it << "\t";

        return 0;
    }

But the result of running this code is always apple Program ended with exit code: 0. I searched but seem not to be able to find where it went wrong. Can anybody help me out? Thanks!

Nick
  • 21
  • 3
  • http://stackoverflow.com/search?q=%5Bc%2B%2B%5D+read+file+into+vector – Ken White Oct 27 '16 at 00:16
  • Duplicate of something that probably uses [`std::back_inserter`](http://en.cppreference.com/w/cpp/iterator/back_inserter) – user4581301 Oct 27 '16 at 00:20
  • 1
    Yeah, here's one: [How can I use std::copy to read directly from a file stream to a container?](http://stackoverflow.com/questions/7152427/how-can-i-use-stdcopy-to-read-directly-from-a-file-stream-to-a-container) – user4581301 Oct 27 '16 at 00:22
  • Sorry but I think the link you gave me cannot solve my problem, I tried `vector col((istream_iterator(data_file)), istream_iterator()); for (auto i: col) cout << i << "\t";` instead and got correct output, but I still don't get what went wrong with `istream_iterator start(data_file), end;` method. Could you please explain why only the first string `apple` got output? :) – Nick Oct 27 '16 at 04:28
  • `for (auto it = start; it != end; it++) cout << *it << "\t";` outputs the contents of the input stream, not the `vector`. The stream was wound all the way to the end of the file reading everything into the `vector`, so the input_iterator had no more file to read in your `for` loop as soon as it was incremented. You could have seeked to the beginning of the file, rewinding the file, but why? You'd already read the file into the `vector`. If I'd been really on the ball, I would have suggested what you did: Print the `vector`. I apologize for the dupe tag. You had already read into the `vector`. – user4581301 Oct 27 '16 at 05:12
  • [Here is your original code with the read into the `vector` commented out, and the file replaced by a stringstream.](https://ideone.com/wRmMvw) You can see that without the `vector` eating up the file you do print out the correct data. . – user4581301 Oct 27 '16 at 05:17
  • Your explanation perfectly solved my problem! I used to think that `for (auto it = start; it != end; it++) cout << *it << "\t";` is the way to print the vector, haha, I am a C++ rookie ;) – Nick Oct 27 '16 at 06:05
  • That is the correct way, or it was before C++11 and still works just fine, to walk an iterator, the thing is `start` is an iterator for the file, not the `vector`. Iteration logic correct, used wrong iterator. – user4581301 Oct 27 '16 at 18:08

0 Answers0