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!