Read from txt file and split data into categories

  arrays, c++, for-loop, readfile, txt

I am trying to write C++ code, in which I am supposed to read a paragraph in a .txt file, and then split the data into arrays. The .txt file contains the data of various alpha decay events (about 2000).

Each event contains:
an event identifier (i.e. an int)
the coordinates x, y , z of the decay point (3 floats)
the number of particles (an int)

for each particle 4 numbers:
the electric charge (an int)
the momentum components p x , p y , p z (3 floats)

An exemple of event is:

2  [head]
0.450944 -0.322479 10.2081
2
1 0.358448 -0.219298 6.3246
-1 -0.0290807 0.0151297 0.831178

I am trying to write a Function "read" taking as arguments:

  • the reference to an input file stream,
  • the reference to 3 floats to fill with the decay point coordinates,
  • an array of integers to fill with electric charges,
  • 3 arrays of float to fill with momenta components
    and returning the number of particles.
#include <iostream>
#include <fstream>

using namespace std;

int  read( ifstream& file, //prototype
           float &x,
       float &y,
       float &z,
       int charges,
       float p1,
       float p2,
       float p3
     );

I can’t figure out how to write the definition of the function so that it can divide the data of each event into the various arrays and variables (the charges in the loaded array, the moments in the moments array, etc.) and return the int value of the particles formed at each event.

Source: Windows Questions C++

LEAVE A COMMENT