有没有人看到,为什么fprintf在printf工作正常时不写入文件?
FILE* database;
database = fopen("database.txt", "a+");
for (x=-200; x<200; x=x+10)
{
TH1F *hist_Deg = new TH1F("h1", "title", 250, -0.5, 249.5);
for (j=5000; j<5010; j++)
{
sprintf(filename,"electronsrough/105.000000MeV/x%d/y0/process%d.txt", x,j);
FILE* pfile =fopen(filename,"r");
while(!feof(pfile))
{
fscanf(pfile, "%lf /n", &p);
hist_Deg->Fill(p);
}
fclose(pfile);
}
meana = hist_Deg->GetMean();
f->SetPoint(i, x, meana);
i=i+1;
fprintf(database, "%f\n", meana);
printf("%f\n", meana);
delete hist_Deg;
}
printf将meana输出到comman行,但是
文件“database.txt”已创建,但仍为空。
答案 0 :(得分:3)
刷新和关闭数据库文件应该可以完成这项工作。
fprintf(database, "%f\n", meana);
fflush(database);
fclose(database);