LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 582|回复: 1

请教程序

[复制链接]
发表于 2005-12-18 11:48:06 | 显示全部楼层 |阅读模式
// 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[4];
Char_t Nation[3];
};

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[80];

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[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);
tree->Fill();
}
tree->rint();
tree->Write();

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

在程序中 char line[80];定义了一个字符数组,长度为80
程序
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);
tree->Fill();
}

1。while (fgets(&line,80,fp))意思是什么?
2。 sscanf(&line[0],"%d %d %d %d %d",&staff.Category,&staff.Flag,&staff.Age,&staff.Service,&staff.Children);
中的&line[0]更是不明白了?请问是什么意思?
3。sscanf(&line[32],"%d %d %d %d %s %s",&staff.Grade,&staff.Step,&staff.Hrweek,&staff.Cost,staff.Division,staff.Nation);
中的&line[32]同样不明白,请问是什么意思?
谢谢phenixwutao!
发表于 2005-12-23 13:26:55 | 显示全部楼层
这也要这样问?

1、是从文件句柄中读取80个字符长度的内容,如果能读到就继续,否则退出
2、就是后面的数据按格式输入到line,msdn中应该也可以查到阿!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表