c语言判断打开文件是否为空的方法

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 !\n");
        return ;
    }
    //printf("%d\n",ftell(fp));

    fgetpos(fp,&pos);
    //printf("pos = %d\n",pos);
    fseek(fp,0L,SEEK_END);
    fgetpos(fp,&endpos);
    //printf("endpos=%d\n",endpos);

    if(pos == endpos){//文件为空,写入标题:方法名 平均时间 最大值 最小值 循环次数 读写大小
        fprintf(fp,"%-30s %-20s %-20s %-20s %-10s %-10s\n","caseName","averageTime(ms)","maxTime(ms)","minTime(ms)","loops","size(KB)");
    }
    fseek(fp,0L,SEEK_END);
    fprintf(fp,"%-30s %-20f %-20f %-20f %-10d %-10d\n",caseName,averageTime,max,min,loops,size);
    fclose(fp);

}

通过文件打开的指正位置,以及结尾的指正位置判断

posted on 2016-08-24 10:58  IT小不点  阅读(5082)  评论(0编辑  收藏  举报