reading from file

Hey! So I recently started to learn c++. I'm now working with reading from file tasks. I now know how to read all the information from .txt files but im stuck at
making the integers i read from the file to variables. like if the first line in my failiukas.txt is 2 0 2 and i want to put those numbers to variables x, y, z. how to do it?

1
2
3
4
5
6
7
8
9
10
      string line;
    ifstream file ("failiukas.txt");
    if(file.is_open())
    {
        while(getline(file,line))
        {
            cout << line << '\n';
        }
        file.close();
    }
#include <sstream>
7
8
9
10
11
std::istringstream iss (line);
if(iss >> x >> y >> z)
{
    //success
}
Last edited on
thanks for your answer!

but if my file is:

1
2
3
5
2 3
5 0 7


will it work? as i think u always try to get 3 parameters from file
Last edited on
OK, that is not what you originally suggested in your first post. Can each line have any number of values on it?
7
8
9
10
11
12
std::istringstream iss (line);
std::vector<int> v;
while(iss >> x)
{
    v.push_back(x);
}
sorry. my bad. yes it can have any number of values.
Did I answer your question then? It's not clear.
How all the code should look like?
Topic archived. No new replies allowed.