LINUX C 数据文件的添加,修改,删除 实例

 

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include<sys/types.h>
  6. #include<sys/timeb.h>
  7. main()
  8. {
  9.     char *pp[] = {"Hongkong123",
  10.                     "Singgapore456",
  11.                     "Beijing",
  12.                     "London",
  13.                     "Washington"
  14.                     }; 
  15.     AddDataFile("./config/ppp.conf",":",pp,5);
  16.     //AlterDataFlie("./config/ppp.conf",":",pp,5,"bclb4eyr0");
  17.     //DeleteDataFile("./config/ppp.conf",":","bclb4eyr0");
  18. }
  19. /*
  20.  *对数据文件进行添加
  21.  *参数1,数据文件路径,参数二,行内数据分隔符,参数三,传入数据指针数组,参数四,数组下标个数
  22.  *
  23.  */
  24. int AddDataFile(char *af_file_path,char *af_sign,char **pp,int *array_sum)
  25. {
  26.     /*
  27.      *随机生成一个5到8位的随机数
  28.      */
  29.     unsigned int seedVal;
  30.     struct timeb timeBuf;
  31.     ftime(&timeBuf);
  32.     seedVal=((((unsigned int)timeBuf.time&0xFFFF)+
  33.                  (unsigned int)timeBuf.millitm)^
  34.                  (unsigned int)timeBuf.millitm);
  35.     srand((unsigned int)seedVal);
  36.     char buffer[10];
  37.     memset(buffer,0,sizeof(buffer));
  38.     int len = (rand() % (9-6))+ 7;
  39.     static const char string[]= "0123456789abcdefghiljklnopqrstuvwxyz";
  40.     int i;
  41.     for(i = 0; i < len; i++)
  42.     {
  43.         buffer[i] = string[rand()% (strlen(string)-1)]; 
  44.     }
  45.     char chnew_filebuf[256];
  46.     memset(chnew_filebuf,0,sizeof(chnew_filebuf));
  47.     strcat(chnew_filebuf,buffer);
  48.     int j;
  49.     for(j = 0;j < array_sum ; j++)
  50.     {
  51.         strcat(chnew_filebuf,af_sign);
  52.         strcat(chnew_filebuf,pp[j]);
  53.     }
  54.     printf("------->%s<---------/n",chnew_filebuf);
  55.     FILE *f;
  56.     f = fopen(af_file_path,"r+");
  57.     fseek(f,0,SEEK_END);
  58.     fseek(f,-1,SEEK_CUR);
  59.     if (fgetc(f)== 10)
  60.     {
  61.         fseek(f,0,SEEK_END);
  62.         fputs(chnew_filebuf,f);
  63.     }
  64.     else
  65.     {
  66.         fseek(f,0,SEEK_END);
  67.         fputs("/n",f);
  68.         fputs(chnew_filebuf,f);
  69.     }
  70.     fclose(f);
  71. }
  72. /*
  73.  *对数据文件进行修改
  74.  *参数1,数据文件路径,参数二,行内数据分隔符,参数三,传入数据指针数组,参数四,数组下标个数,参数五,修改标记值
  75.  */
  76. int AlterDataFlie(char *alf_file_path,char *alf_sign,char **pp,int *array_sum,char *al_linesign)
  77. {
  78.     char chnew_filebuf[256];
  79.     char datafile_linebuf[256];
  80.     char line_name[40];
  81.     char *leave_line;
  82.     memset(chnew_filebuf,0,sizeof(chnew_filebuf));
  83.     strcat(chnew_filebuf,al_linesign);
  84.     int i;
  85.     for(i = 0;i < array_sum ; i++)
  86.     {
  87.         strcat(chnew_filebuf,alf_sign);
  88.         strcat(chnew_filebuf,pp[i]);
  89.     }
  90.     printf("------->%s<---------/n",chnew_filebuf);
  91.     FILE *f;
  92.     f = fopen(alf_file_path,"r+");
  93.     if(f == NULL)
  94.     {
  95.         printf("OPEN CONFIG FALID/n");
  96.         return 0;
  97.     }
  98.     fseek(f,0,SEEK_END);
  99.     long datafile_lenth = ftell(f);
  100.     int datafilebuf_lenth = strlen(chnew_filebuf);
  101.     char sum_buf[datafile_lenth+datafilebuf_lenth];
  102.     memset(sum_buf,0,sizeof(sum_buf));
  103.     fseek(f,0,SEEK_SET); 
  104.     while(fgets(datafile_linebuf,256,f) != NULL)
  105.     {   
  106.         if(strlen(datafile_linebuf) < 3) //判断是否是空行
  107.         {
  108.             strcat(sum_buf,datafile_linebuf);
  109.             continue;
  110.         }
  111.         leave_line = NULL;
  112.         leave_line = strstr(datafile_linebuf,alf_sign);
  113.         if(leave_line == NULL)                            //去除无标记的情况
  114.         {
  115.             strcat(sum_buf,datafile_linebuf);
  116.             continue;
  117.         }
  118.         int leave_num = leave_line - datafile_linebuf;
  119.         memset(line_name,0,sizeof(line_name));
  120.         strncpy(line_name,datafile_linebuf,leave_num);
  121.         if(strcmp(line_name,al_linesign) ==0)
  122.         {
  123.             strcat(sum_buf,chnew_filebuf);
  124.             strcat(sum_buf,"/n");
  125.         }
  126.         else
  127.         {
  128.             strcat(sum_buf,datafile_linebuf);
  129.         }
  130.         if(fgetc(f)==EOF)
  131.         {
  132.             break;  
  133.         }
  134.         fseek(f,-1,SEEK_CUR);
  135.         memset(datafile_linebuf,0,sizeof(datafile_linebuf));
  136.     }
  137.     printf("----->%s<------/n",sum_buf);
  138.     
  139.     remove(alf_file_path);
  140.     fclose(f);
  141.     FILE *fp;
  142.     fp = fopen(alf_file_path,"w+");
  143.     if(fp == NULL)
  144.     {
  145.         printf("OPEN CONFIG FALID/n");
  146.         return 2;
  147.     }
  148.     fseek(fp,0,SEEK_SET);
  149.     fputs(sum_buf,fp);
  150.     fclose(fp);
  151.     
  152. }
  153. /*
  154.  *对数据文件进行删除
  155.  *参数1,数据文件路径,参数二,行内数据分隔符,参数三,删除标记值
  156.  */
  157. int DeleteDataFile(char *dlf_file_path,char *dlf_sign,char *dl_linesign)
  158. {
  159.     char datafile_linebuf[256];
  160.     char line_name[40];
  161.     char *leave_line;
  162.     
  163.     FILE *f;
  164.     f = fopen(dlf_file_path,"r+");
  165.     if(f == NULL)
  166.     {
  167.         printf("OPEN CONFIG FALID/n");
  168.         return 0;
  169.     }
  170.     fseek(f,0,SEEK_END);
  171.     long datafile_lenth = ftell(f);
  172.     char sum_buf[datafile_lenth];
  173.     memset(sum_buf,0,sizeof(sum_buf));
  174.     fseek(f,0,SEEK_SET); 
  175.     while(fgets(datafile_linebuf,256,f) != NULL)
  176.     {   
  177.         if(strlen(datafile_linebuf) < 3) //判断是否是空行
  178.         {
  179.             strcat(sum_buf,datafile_linebuf);
  180.             continue;
  181.         }
  182.         leave_line = NULL;
  183.         leave_line = strstr(datafile_linebuf,dlf_sign);
  184.         if(leave_line == NULL)                            //去除无标记的情况
  185.         {
  186.             strcat(sum_buf,datafile_linebuf);
  187.             continue;
  188.         }
  189.         int leave_num = leave_line - datafile_linebuf;
  190.         memset(line_name,0,sizeof(line_name));
  191.         strncpy(line_name,datafile_linebuf,leave_num);
  192.         if(strcmp(line_name,dl_linesign) ==0)
  193.         {
  194.             
  195.         }
  196.         else
  197.         {
  198.             strcat(sum_buf,datafile_linebuf);
  199.         }
  200.         
  201.         if(fgetc(f)==EOF)
  202.         {
  203.             break;  
  204.         }
  205.         fseek(f,-1,SEEK_CUR);
  206.         memset(datafile_linebuf,0,sizeof(datafile_linebuf));
  207.     }
  208.     printf("---sum_buf---->%s<----------/n",sum_buf);
  209.     remove(dlf_file_path);
  210.     fclose(f);
  211.     FILE *fp;
  212.     fp = fopen(dlf_file_path,"w+");
  213.     if(fp == NULL)
  214.     {
  215.         printf("OPEN CONFIG FALID/n");
  216.         return 2;
  217.     }
  218.     fseek(fp,0,SEEK_SET);
  219.     fputs(sum_buf,fp);
  220.     fclose(fp);
  221. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值