Re: [ROOT] Reading file

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Jun 25 2003 - 15:15:14 MEST


Hi Livia,

your last column requires a number with 13 significative digits.
You cannot read this column with a simple 32 bit integer (int timestamp).
You could replace the declaration
int timestamp
by
long long timestamp
however, the I/O functions of the interpreter like sscanf, printf or cout
will not be able to input/output this number correctly.
You will find below a modified version of your script filling
a variable declared "Long64_t timestamp. It is a 64 bit integer correctly
filled in the loop. Now, up to you to be carefull in using this variable.
You can for example execute this script and then at the command line
type
root > timestamp
this will show the last value of timstamp from the file

Rene Brun

{
  gROOT->Reset();

  int id;
  char name1[10];
  char name2[10];
  int type;
  float measure;
  Long64_t timestamp;
  int time,stamp;
  char temp[200];
  FILE *apre;

  apre = fopen("extprobe.dat","r");

  while(fgets(temp, sizeof(temp), apre) !=NULL) {

    sscanf(temp, "%d %s %s %d %g %6d%7d",
    &id, &name1,&name2,  &type, &measure, &time,&stamp);
    timestamp = time*100000000LL + stamp;
    printf("%d %s %s %d %g %6d%7d\n",
    id, name1,name2, type, measure, time,stamp);
  }

    fclose(apre);
}

Livia FERRO wrote:
> 
> Hello,
> 
> I'm a Summer Student and I've a problem with root.
> I would like to read a data file like this:
> 
> 25 ext probe 1 21.93 1052989373314
> 26 ext probe 1 19.95 1052989373362
> 27 ext probe 1 20.25 1052989373418
> 28 ext probe 1 20.40 1052989373465
> 29 ext probe 1 20.36 1052989373517
> 30 ext probe 1 19.99 1052989373620
> 31 ext probe 1 21.17 1052989373683
> 
> In order to do this, I have written a script:
> 
> {
>   gROOT->Reset();
> 
>   int id;
>   char name1[10];
>   char name2[10];
>   int type;
>   float measure;
>   int timestamp;
>   char temp[200];
>   FILE *apre;
> 
>   apre = fopen("extprobe.dat","r");
> 
>   while(fgets(temp, sizeof(temp), apre) !=NULL) {
> 
>     sscanf(temp, "%d %s %s %d %g %d",
>     &id, &name1,&name2,  &type, &measure, &timestamp);
>     printf("%d %s %s %d %g %d\n",
>     id, name1,name2, type, measure, timestamp);
> 
>   }
> 
>     fclose(apre);
> }
> 
> Alas, it doesn't work, because it can't manage to read the last number
> of each line (it's too long).
> Could you give any hint?
> 
> Thank you very much.
> 
> Livia Ferro



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:12 MET