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: 
The line:

```
getline(ss, token, ' ');

```

Extracts the line up to the space. Then the next call to `getline` starts at the next line, totally skipping any further input on the previous line. 

What you want is to extract each element of the line. This can be accomplished by using a simple `while` loop:

```
    while (ss >> token)
        temp_vec.push_back(token);

```

Note you do not need to know how many items are separated by spaces. Thus if you have more than 3 items per line, the loop will still work correctly without changing code.
```
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

```