I’m trying to read data values from a text file and store them in a data structure. I could achieve something like this fairly simply in Java but I am unsure how to best approach the problem.
A line of data is x, y, sigma like 1.1 1.2 1.3. I know how to get the entire line into a string in c++, using getline(myFile, string) but I’m not sure if this is the correct approach or if it is, where to go from there. Is it possible to split the line then parse the split strings as double values?
My data structure looks like this:
struct datapoint {
double x;
double y;
double sigma;
double weight;
double xSquared;
double xy;
};
My questions are these:
Is there a way to parse numbers into structures, where each line of the .txt file is a struct in an array of datapoint?
If there is no direct method, are there equivalents in c++ of Java’s split() and Double.parseDouble(string) methods?
Thanks.
Just reading them in with
might be the easiest solution for your problem…
However, if you really need to do the splitting, have a look here: http://www.cplusplus.com/faq/sequences/strings/split/