Reading two different lines from a file

Hi!

I have a file where information are stored the following way:

trackin 0.0 0.0 0.0 0.0
weight 0.0 0.0

trackin and weight keep repeating (their values not being all 0.0) thousands times during the file.
I need to read the fourth number in trackin and the second in weight. Then I need to multiply them by each other every time to get a final numer. I do this as follows


char line[50] = "trackin";
char line2[50] = "weight";
float en_in, f4, en, wei, w2, w3;

while(!feof(input_file)){

   fscanf (input_file, "%s",en);
   if(strcmp (line, en) == 0){
  fscanf (pFile[k],"%*f %*f %f", &f4);

   en_in = f4;  
   }

fscanf (input_file, “%s”,wei);

if(strcmp (line2, wei) == 0){

fscanf (pFile[k],"%*f %f ", &w2); 
   w3 = en_in * w2;
   w3 = log10(w3);

   cout<< " EN_IN BEFORE = "<<en_in<<endl;
   en_in = log10(en_in);
   cout<< "EN_IN AFTER = "<<en_in<<endl;
 }

I get back the following problem:

EN_IN BEFORE= 8.84795e-10
EN_IN AFTER = -9.05316
EN_IN BEFORE = -9.05316
Error: log10 param[0]=-9.05316 up:inf low:0 out of range FILE:nt_he.C LINE:201
*** Interpreter error recovered ***

It looks as though in the second “if” the en_in value read is not f4, but rather the en_in value left after the previous log(10).

So can I read values from two different lines in the the same file and use them for calculation in the same while? Otherwise what could I do?

Hi,
you have plenty of errors in your C code. See attached macro, which is C++ and which works. Please note that at least I prefer answering ROOT questions on this board instead of fixing other people’s C(++) errors…
Axel.
data.txt (61.7 KB)
test.C (2.18 KB)