Question: Given the text file input:

```
s_1 s_2 1
s_3 s_4 2
s_5 s_6 3

```

I would like to have a vector that stores each string element into the vector. Please keep in mind that I've been researching other posts, but I couldn't manage to seperate by **whitespace**.

Here is my code:

```
#include<iostream>
using std::cout; using std::endl;
#include<fstream>
using std::ifstream;
#include<vector>
using std::vector;
#include<string>
using std::string; using std::getline;
#include<sstream>
using std::stringstream; using std::ostringstream; using std::istringstream;

void read_file(const string &fname) {
    string file;
    string line;
    vector<string> temp_vec;

    file = fname;
    ifstream in_file;

    in_file.open(file);

    while(getline(in_file, line)) {
        istringstream ss(line);
        string token;
        getline(ss, token, ' ');
        temp_vec.push_back(token);
    }

    for (string ele:temp_vec) { //print the content of the vector
        cout << ele << endl;
    }
}

int main() {
    read_file("text_file.txt"); //insert your file path here
}

```

My error in this code is that the following are printed:

```
s_1
s_3
s_5

```

However, I would like the vector to store each element and be:

**{s\_1, s\_2, 1, s\_3, s\_4, 2, s\_5, s\_6, 3}**

Answer: 
```cpp
#include<iostream>
using std::cout; using std::endl;
#include<fstream>
using std::ifstream;
#include<vector>
using std::vector;
#include<string>
using std::string;

void read_file(const string &fname) {
    ifstream in_file(fname);
    string token;
    vector<string> temp_vec;

    while( in_file.good() ) {
        in_file >> token;
        temp_vec.push_back(token);
    }

    for (string ele: temp_vec) { //print the content of the vector
        cout << ele << endl;
    }
}

int main() {
    read_file("text_file.txt"); //insert your file path here
}

```
```
string token1, token2, token3;
while(getline(in_file, line)) {
    istringstream ss(line);
    ss >> token1 >> token2 >> token3;
    temp_vec.push_back(token1);
    temp_vec.push_back(token2);
    temp_vec.push_back(token3);
}

cout << "{";
for (auto &ele : temp_vec) { //print the content of the vector
    cout << ele;
    if (&ele != temp_vec.data() + temp_vec.size() - 1) {
        cout << ", ";
    }
}
cout << "}" << endl

```