Hi, "Axel Naumann" <axel-naumann@gmx.de> wrote concerning Re: [ROOT] Header in ascii file [Thu, 16 Oct 2003 15:05:34 +0200 (CEST)] ---------------------------------------------------------------------- > Hi, > > now that's simple. The easiest way (using an excerpt from staff.C as an > example): > > int line=0; > while (fgets(&line,80,fp)) { > sscanf(&line[0],"%d %d %d %d > %d",&staff.Category,&staff.Flag,&staff.Age,&staff.Service,&staff.Children); > sscanf(&line[32],"%d %d %d %d %s > %s",&staff.Grade,&staff.Step,&staff.Hrweek,&staff.Cost,staff.Division,staff.Nation); > if (line++)>6) tree->Fill(); > } > Why not use proper C++ constructs? #ifndef __CINT__ #ifndef __IOSTREAM__ #include <iostream> #endif #ifndef ROOT_TTree #include "TTree.h" #endif #endif #ifndef __STRING__ #include <string> #endif #ifndef __FSTREAM__ #include <fstream> #endif using namespace std; struct person { int category; int flag; int age; int service; int children; int grade; int step; int hours_per_week; int cost; char division[4]; char nationality[3]; }; istream& operator>>(istream& in, person& p) { return in >> p.category >> p.flag >> p.age >> p.service >> p.children >> p.grade >> p.step >> p.hours_per_week >> p.cost >> p.division >> p.nationality; } TTree* read(const string& filename, size_t skip=6) { TTree* tree; ifstream input(filename.c_str()); if (!input) { cerr << "bad file" << endl; return 0; } for (size_t i = 0; i < skip; i++) { string line; getline(input, string); } person p; tree = new TTree("T", filename.c_str()); tree->Branch("staff",&p.category, "Category/I:Flag:Age:Service:Children:" "Grade:Step:Hrweek:Cost"); tree->Branch("Division",p.division,"Division/C", 32000); tree->Branch("Nation",p.nationality,"Nation/C", 32000); while (true) { input >> p; if (input.eof()) break; if (input.bad()) { cerr << "bad read" << endl; delete tree; tree = 0; } tree->Fill(); } input.close(); return tree; } Much more pedagogical I think. I really think the examples in ROOT should avoid C/C-like/Fortran-like constructs (sscanf being the case at hand), and promote more OO kind of programming. Yours, ___ | Christian Holm Christensen |_| | ------------------------------------------------------------- | | Address: Sankt Hansgade 23, 1. th. Phone: (+45) 35 35 96 91 _| DK-2200 Copenhagen N Cell: (+45) 24 61 85 91 _| Denmark Office: (+45) 353 25 404 ____| Email: cholm@nbi.dk Web: www.nbi.dk/~cholm | |
This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:16 MET