最近工作中读取对AC或者AP产生的Event的文件并发送ACS后删除已发送的内容,下次发送未发送event。产生的event存入event文件中,要对event.dat文件进行文件操作,以下是操作流程!
void ParseEvent(void)
{
FILE *fp,*dfp;
char cp[1024],dir[128],ddir[128];
struct stat buf;
memset(dir,0,sizeof(dir));
memset(ddir,0,sizeof(ddir));
sprintf(dir,"event.dat");
sprintf(ddir,"event.dat.bak");
if(!access(dir,F_OK)){
fp = fopen(dir,"r+");
dfp = fopen(ddir,"w+");
if(fp && dfp)
{
while (fgets (cp, sizeof(cp), fp) != NULL)
{
// remove the line start with '#', ' ', '\n', '\0'
if ( cp[0] == '#' || cp[0] == ' ' || cp[0] == '\n' || cp[0] == '\0' )
{
continue;
}
// read NUM_LINES lines from the file
if( ++ec < NUM_LINES)
{
FireEvent(cp);
}else{
fprintf(dfp,"%s\n",cp);
}
}
fclose(fp);
fclose(dfp);
// sync all the buffer or cache to the file system
sync();
// rename() if the file exist, the file will be replaced.
rename(ddir,dir);
}
}
}