/*
Name: 判断程序是第几次被打开
Copyright:
Author: 随心
Date: 29-10-07 19:34
Description:
*/
#include <stdio.h>
#include <stdlib.h>
#define BEGIN 0 //初始值
int build()
{
FILE *fp;
char maxsz[100];
int cnt=BEGIN;
if((fp=fopen("D:\\CNT.DLL","w"))!=NULL)
{
sprintf(maxsz,"%d",cnt);
fprintf(fp,maxsz);
}
fclose(fp);
return cnt;
}
int count()
{
FILE *fr;
char c[100],ww[100],*pc=c;
int count=0;
if((fr=fopen("D:\\CNT.DLL","r"))!=NULL) //这部用来读
{
rewind(fr); //确保指针在文件头
fgets(c,20,fr);
count=atoi(pc);
fclose(fr);
}
else
count=build();
FILE *fw;
++count;
if( (fw=fopen("D:\\CNT.DLL","w"))!=NULL) //这部分用来重写文件
{
sprintf(ww,"%d",count);
fprintf(fw,ww);
fclose(fw);
}
return count;
}
int main()
{
printf("%d\n",count());
system("pause");
return 0;
}
----------------解决方案--------------------------------------------------------