- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include<sys/types.h>
- #include<sys/timeb.h>
- main()
- {
- char *pp[] = {"Hongkong123",
- "Singgapore456",
- "Beijing",
- "London",
- "Washington"
- };
- AddDataFile("./config/ppp.conf",":",pp,5);
- //AlterDataFlie("./config/ppp.conf",":",pp,5,"bclb4eyr0");
- //DeleteDataFile("./config/ppp.conf",":","bclb4eyr0");
- }
- /*
- *对数据文件进行添加
- *参数1,数据文件路径,参数二,行内数据分隔符,参数三,传入数据指针数组,参数四,数组下标个数
- *
- */
- int AddDataFile(char *af_file_path,char *af_sign,char **pp,int *array_sum)
- {
- /*
- *随机生成一个5到8位的随机数
- */
- unsigned int seedVal;
- struct timeb timeBuf;
- ftime(&timeBuf);
- seedVal=((((unsigned int)timeBuf.time&0xFFFF)+
- (unsigned int)timeBuf.millitm)^
- (unsigned int)timeBuf.millitm);
- srand((unsigned int)seedVal);
- char buffer[10];
- memset(buffer,0,sizeof(buffer));
- int len = (rand() % (9-6))+ 7;
- static const char string[]= "0123456789abcdefghiljklnopqrstuvwxyz";
- int i;
- for(i = 0; i < len; i++)
- {
- buffer[i] = string[rand()% (strlen(string)-1)];
- }
- char chnew_filebuf[256];
- memset(chnew_filebuf,0,sizeof(chnew_filebuf));
- strcat(chnew_filebuf,buffer);
- int j;
- for(j = 0;j < array_sum ; j++)
- {
- strcat(chnew_filebuf,af_sign);
- strcat(chnew_filebuf,pp[j]);
- }
- printf("------->%s<---------/n",chnew_filebuf);
- FILE *f;
- f = fopen(af_file_path,"r+");
- fseek(f,0,SEEK_END);
- fseek(f,-1,SEEK_CUR);
- if (fgetc(f)== 10)
- {
- fseek(f,0,SEEK_END);
- fputs(chnew_filebuf,f);
- }
- else
- {
- fseek(f,0,SEEK_END);
- fputs("/n",f);
- fputs(chnew_filebuf,f);
- }
- fclose(f);
- }
- /*
- *对数据文件进行修改
- *参数1,数据文件路径,参数二,行内数据分隔符,参数三,传入数据指针数组,参数四,数组下标个数,参数五,修改标记值
- */
- int AlterDataFlie(char *alf_file_path,char *alf_sign,char **pp,int *array_sum,char *al_linesign)
- {
- char chnew_filebuf[256];
- char datafile_linebuf[256];
- char line_name[40];
- char *leave_line;
- memset(chnew_filebuf,0,sizeof(chnew_filebuf));
- strcat(chnew_filebuf,al_linesign);
- int i;
- for(i = 0;i < array_sum ; i++)
- {
- strcat(chnew_filebuf,alf_sign);
- strcat(chnew_filebuf,pp[i]);
- }
- printf("------->%s<---------/n",chnew_filebuf);
- FILE *f;
- f = fopen(alf_file_path,"r+");
- if(f == NULL)
- {
- printf("OPEN CONFIG FALID/n");
- return 0;
- }
- fseek(f,0,SEEK_END);
- long datafile_lenth = ftell(f);
- int datafilebuf_lenth = strlen(chnew_filebuf);
- char sum_buf[datafile_lenth+datafilebuf_lenth];
- memset(sum_buf,0,sizeof(sum_buf));
- fseek(f,0,SEEK_SET);
- while(fgets(datafile_linebuf,256,f) != NULL)
- {
- if(strlen(datafile_linebuf) < 3) //判断是否是空行
- {
- strcat(sum_buf,datafile_linebuf);
- continue;
- }
- leave_line = NULL;
- leave_line = strstr(datafile_linebuf,alf_sign);
- if(leave_line == NULL) //去除无标记的情况
- {
- strcat(sum_buf,datafile_linebuf);
- continue;
- }
- int leave_num = leave_line - datafile_linebuf;
- memset(line_name,0,sizeof(line_name));
- strncpy(line_name,datafile_linebuf,leave_num);
- if(strcmp(line_name,al_linesign) ==0)
- {
- strcat(sum_buf,chnew_filebuf);
- strcat(sum_buf,"/n");
- }
- else
- {
- strcat(sum_buf,datafile_linebuf);
- }
- if(fgetc(f)==EOF)
- {
- break;
- }
- fseek(f,-1,SEEK_CUR);
- memset(datafile_linebuf,0,sizeof(datafile_linebuf));
- }
- printf("----->%s<------/n",sum_buf);
- remove(alf_file_path);
- fclose(f);
- FILE *fp;
- fp = fopen(alf_file_path,"w+");
- if(fp == NULL)
- {
- printf("OPEN CONFIG FALID/n");
- return 2;
- }
- fseek(fp,0,SEEK_SET);
- fputs(sum_buf,fp);
- fclose(fp);
- }
- /*
- *对数据文件进行删除
- *参数1,数据文件路径,参数二,行内数据分隔符,参数三,删除标记值
- */
- int DeleteDataFile(char *dlf_file_path,char *dlf_sign,char *dl_linesign)
- {
- char datafile_linebuf[256];
- char line_name[40];
- char *leave_line;
- FILE *f;
- f = fopen(dlf_file_path,"r+");
- if(f == NULL)
- {
- printf("OPEN CONFIG FALID/n");
- return 0;
- }
- fseek(f,0,SEEK_END);
- long datafile_lenth = ftell(f);
- char sum_buf[datafile_lenth];
- memset(sum_buf,0,sizeof(sum_buf));
- fseek(f,0,SEEK_SET);
- while(fgets(datafile_linebuf,256,f) != NULL)
- {
- if(strlen(datafile_linebuf) < 3) //判断是否是空行
- {
- strcat(sum_buf,datafile_linebuf);
- continue;
- }
- leave_line = NULL;
- leave_line = strstr(datafile_linebuf,dlf_sign);
- if(leave_line == NULL) //去除无标记的情况
- {
- strcat(sum_buf,datafile_linebuf);
- continue;
- }
- int leave_num = leave_line - datafile_linebuf;
- memset(line_name,0,sizeof(line_name));
- strncpy(line_name,datafile_linebuf,leave_num);
- if(strcmp(line_name,dl_linesign) ==0)
- {
- }
- else
- {
- strcat(sum_buf,datafile_linebuf);
- }
- if(fgetc(f)==EOF)
- {
- break;
- }
- fseek(f,-1,SEEK_CUR);
- memset(datafile_linebuf,0,sizeof(datafile_linebuf));
- }
- printf("---sum_buf---->%s<----------/n",sum_buf);
- remove(dlf_file_path);
- fclose(f);
- FILE *fp;
- fp = fopen(dlf_file_path,"w+");
- if(fp == NULL)
- {
- printf("OPEN CONFIG FALID/n");
- return 2;
- }
- fseek(fp,0,SEEK_SET);
- fputs(sum_buf,fp);
- fclose(fp);
- }