input file to objects member variables

suppose i have a file with info of

bodyname(string) bodyx(double) bodyy(double) bodyz(double)
bodyname(string) bodyx(double) bodyy(double) bodyz(double)
bodyname(string) bodyx(double) bodyy(double) bodyz(double)

(there is an unknown amount of whitespace (can be tab/space) between each different token)
and i have a body class that requires a string, double, double, double.

how would i fill an array of the body class's member variables with the input from the given file?
Last edited on
The tricky thing is having that string at the beginning like that. How can we determine where the string ends?
the string ends once white space is found. the strings are the token at the beginning of every new line. That will never change
Last edited on
What if the string has white space in it?
the strings will not have whitespace. users of the file identified spaces with an underscore.
That's ok then. This shows how you might read the file.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
#include <string>

int main()
{
        std::string s;
        double d1, d2;

        std::ifstream in("in.txt");
        while (in >> s >> d1 >> d2)
                std::cout << s << ':' << d1 << ':' << d2 << std::endl;
}
Last edited on
Topic archived. No new replies allowed.