void StoreEvent(char *str)
{
long tt;
struct tm *vtm;
int i;
char Etime[100];
char filename[20];
char dirname[20];
char path[100];
getcwd(path, sizeof(path));
time( &tt );
vtm = localtime( &tt );
sprintf(Etime,"%d-%02d-%02d %02d:%02d:%02d ",vtm->tm_year+1900,vtm->tm_mon+1,vtm->tm_mday,vtm->tm_hour,vtm->tm_min,vtm->tm_sec);
sprintf(filename,"/%d%02d%02d.txt",vtm->tm_year+1900,vtm->tm_mon+1,vtm->tm_mday);
sprintf(dirname,"/%d%02d",vtm->tm_year+1900,vtm->tm_mon+1);
strcat(path,dirname);
if(access(path,0)==-1) //access函数是查看文件是不是存在
{
if (mkdir(path,0777)) //如果不存在就用mkdir函数来创建
{
printf("creat file bag failed!!!\n");
}
}
strcat(path,filename);
FILE *fp;
fp = fopen(path,"ar+");
if(fp == NULL)
{
printf("文件异常。\n");
return;
}
lseek(fp,0,SEEK_END); //将读写位置移到文件尾
strcat(Etime,str);
fputs(Etime,fp);
fflush(fp);
fsync(fileno(fp)); //同步缓存
fclose(fp);
}
原文地址:https://www.cnblogs.com/lijf/p/10396014.html