void writeReslut2(char* caseName,double averageTime,double max, double min,int loops,int size){ fpos_t pos = 0; fpos_t endpos = 0; FILE *fp = fopen("E:\PerformanceResult.txt","at"); if(NULL == fp){ printf("open file failed ! "); return ; } //printf("%d ",ftell(fp)); fgetpos(fp,&pos); //printf("pos = %d ",pos); fseek(fp,0L,SEEK_END); fgetpos(fp,&endpos); //printf("endpos=%d ",endpos); if(pos == endpos){//文件为空,写入标题:方法名 平均时间 最大值 最小值 循环次数 读写大小 fprintf(fp,"%-30s %-20s %-20s %-20s %-10s %-10s ","caseName","averageTime(ms)","maxTime(ms)","minTime(ms)","loops","size(KB)"); } fseek(fp,0L,SEEK_END); fprintf(fp,"%-30s %-20f %-20f %-20f %-10d %-10d ",caseName,averageTime,max,min,loops,size); fclose(fp); }
通过文件打开的指正位置,以及结尾的指正位置判断