Apr 22, 2015 at 7:58pm UTC
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 Apr 22, 2015 at 8:01pm UTC
Apr 23, 2015 at 1:26pm UTC
The tricky thing is having that string at the beginning like that. How can we determine where the string ends?
Apr 23, 2015 at 7:20pm UTC
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 Apr 23, 2015 at 7:20pm UTC
Apr 23, 2015 at 7:30pm UTC
What if the string has white space in it?
Apr 23, 2015 at 7:56pm UTC
the strings will not have whitespace. users of the file identified spaces with an underscore.
Apr 23, 2015 at 8:32pm UTC
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 Apr 23, 2015 at 10:58pm UTC