Linux 读取文件n行并删除方法

来源:互联网 发布:怎么安装mysql完全安装 编辑:程序博客网 时间:2024/06/05 07:23

最近工作中读取对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 fileif( ++ec  < NUM_LINES){FireEvent(cp);}else{fprintf(dfp,"%s\n",cp);}}fclose(fp);fclose(dfp);// sync all the buffer or cache to the file systemsync();// rename() if the file exist, the file will be replaced.rename(ddir,dir);}}}



0 0
原创粉丝点击