Re: fscanf limitation

From: Stephan Heising (stephan.heising@cern.ch)
Date: Thu Dec 09 1999 - 11:39:59 MET


Hello Christophe

fscanf ist not a good choice at all. The position of the file pointer 
when an error is encountered is undefined. Better use:

Char_t Line[300];  // or apropriate
fgets (Line, 300, fpointer); // to read a line including cr at the end
                             // fgets read actually only 299 chars 
plus 0x00
sscanf (Line, Format_string, args); // as fscanf

advantage: the string can be divided as needed prior to
scanning the arguments -> any number of arguments is possible only 
you have to find a convenient way to split the string after a complete 
argument.

Stephan Heising

>>>>>>>>>>>>>>>>>> Ursprüngliche Nachricht <<<<<<<<<<<<<<<<<<

Am 12/8/99, 6:18:25 PM, schrieb Christophe Suire <suire@in2p3.fr> zum 
Thema fscanf limitation:


> Hello,

> I have the following error when I try  to use the macro "reading an
> ascii file " ( look at the end of the mail ) with a large number of
> arguments.


> Limitation: fscanf only takes upto 12 arguments
> FILE:/home/suire/Test_prod/data/./mynt.C LINE:34
> *** Interpreter error recovered ***

> I understand well the error but I cannot reduce the number of 
arguments.
> Any ideas how to do it.

>  Thanks.



> Reading an ascii file and making an ntuple

> {
> //   example of macro to read data from an ascii file and
> //   create a root file with an histogram and an ntuple.

>    gROOT->Reset();

>    FILE *fp = fopen("/user/brun/root/basic.dat","r");

>    Float_t x,y,z;
>    Int_t ncols;
>    Int_t nlines = 0;
>    TFile *f = new TFile("basic.root","RECREATE");
>    TH1F *h1 = new TH1F("h1","x distribution",100,-4,4);
>    TNtuple *ntuple = new TNtuple("ntuple","data from ascii
> file","x:y:z");

>    while (1) {
>       ncols = fscanf(fp,"%f %f %f",&x, &y, &z);
>       if (ncols < 0) break;
>       if (nlines < 5) printf("x=%8f, y=%8f, z=%8f\n",x,y,z);
>       h1->Fill(x);
>       ntuple->Fill(x,y,z);
>       nlines++;
>    }
>    printf(" found %d points\n",nlines);

>    fclose(fp);

>    f->Write();
> }
> --

> -------------------------------------------------
> Christophe Suire                                   suire@in2p3.fr
> Institut de Recherches Subatomiques   phone: +33(0)388106154
> 23,rue du Loess Bat20 BP28                fax:   +33(0)388106614
> F-67037 Strasbourg cedex2
> -------------------------------------------------



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:44 MET