need help on reading in data

This seems like a pretty easy task to implement but I can't figure out the best way of doing it. I have some data that looks like the following:

+ alpha 3.631657 0.911405 0.400035 0.096500
+ gamma 0.057156 0.133028 -0.452153 0.881964
+ He3 3.338188 0.229391 0.971863 0.053501
+ B10[0.0] 7.585255 -0.481513 -0.543344 0.687693

The first entries of each line tell you what kind of particle you are dealing with and the next four entries are energy, x-position, y-position and z-position respectively. I process this data with the getline function:

1
2
3
4
5
ifstream myfile("C:\\file.txt");
while (myfile.good())
{
getline(myfile, line);
}


Next I need to extract the energy and x, y and z positions from each line so that that you would be able to write the parameters from each line like:

1
2
3
4
energy = ....
x_pos = ....
y_pos = ....
z_pos = ...


Is there some function I can use to pull values separated by a space easily? Maybe something similar to the line.find() or line.at() functions?
Last edited on
Why not just extract the data directly??
1
2
3
4
myfile.ignore(); //to get rid of the +
std::string type;
double energy, x, y, z;
myfile >> type >> energy >> x >> y >> z;
Put it in a loop and you're done.
LB, would that not be just writing the information back into myfile? I want to extract these quantities so that I can then use them to perform other calculations.....
>> is extracting FROM the file TO the variables.
<< is writing TO the file FROM the variables.

You can tell by whether the arrows point into or out of the file identifier. The same logic applies to all streams in C++ ;)
Last edited on
Oops of course. I'll give that a try....
Topic archived. No new replies allowed.