lzhxin 发表于 2005-12-18 11:36:24

请教编程!

// example of macro to read data from an ascii file and
// create a root file with a Tree.
// see also a variant in cernbuild.C
// Author: Rene Brun
void staff() {

struct staff_t {
Int_t Category;
UInt_t Flag;
Int_t Age;
Int_t Service;
Int_t Children;
Int_t Grade;
Int_t Step;
Int_t Hrweek;
Int_t Cost;
Char_t Division;
Char_t Nation;
};

staff_t staff;

//The input file cern.dat is a copy of the CERN staff data base
//from 1988
FILE *fp = fopen("cernstaff.dat","r");

char line;

TFile *f = new TFile("staff.root","RECREATE");
TTree *tree = new TTree("T","staff data from ascii file");
tree->Branch("staff",&staff.Category,"Category/I:Flag:Age:Service:Children:Grade:Step:Hrweek:Cost");
tree->Branch("Division",staff.Division,"Division/C");
tree->Branch("Nation",staff.Nation,"Nation/C");
//note that the branches Division and Nation cannot be on the first branch
while (fgets(&line,80,fp)) {
sscanf(&line,"%d %d %d %d %d",&staff.Category,&staff.Flag,&staff.Age,&staff.Service,&staff.Children);
sscanf(&line,"%d %d %d %d %s %s",&staff.Grade,&staff.Step,&staff.Hrweek,&staff.Cost,staff.Division,staff.Nation);
tree->Fill();
}
tree->Print();
tree->Write();

fclose(fp);
delete tree;
delete f;
}

在程序中 char line;定义了一个字符数组,长度为80
程序
while (fgets(&line,80,fp)) {
sscanf(&line,"%d %d %d %d %d",&staff.Category,&staff.Flag,&staff.Age,&staff.Service,&staff.Children);
sscanf(&line,"%d %d %d %d %s %s",&staff.Grade,&staff.Step,&staff.Hrweek,&staff.Cost,staff.Division,staff.Nation);
tree->Fill();
}

1。while (fgets(&line,80,fp))意思是什么?
2。 sscanf(&line,"%d %d %d %d %d",&staff.Category,&staff.Flag,&staff.Age,&staff.Service,&staff.Children);
中的&line更是不明白了?请问是什么意思?
3。sscanf(&line,"%d %d %d %d %s %s",&staff.Grade,&staff.Step,&staff.Hrweek,&staff.Cost,staff.Division,staff.Nation);
中的&line同样不明白,请问是什么意思?
谢谢phenixwutao!

MichaelBibby 发表于 2005-12-19 10:03:32

C还是SHELL?

bison_gao 发表于 2006-1-10 17:44:42

找本C语言基础知识看看,呵呵还有CPP呢。这个问题不是几下可以说清楚的。
页: [1]
查看完整版本: 请教编程!