配置文件的读,添加,修改,删除

  1. /*
  2.  *从配置文件中读取相应的值
  3.  *输入参数:1,配置文件路径 2,匹配标记 3,输出存储空间
  4.  *并且排除了空行,“=”前后无内容,无“=”的情况
  5.  */
  6. ReadConfig(char *conf_path,char *conf_name,char *config_buff)
  7. {
  8.     char config_linebuf[256];
  9.     char line_name[40];
  10.     char exchange_buf[256];
  11.     char *config_sign = "=";
  12.     char *leave_line;
  13.     FILE *f;
  14.     f = fopen(conf_path,"r");
  15.     if(f == NULL)
  16.     {
  17.         printf("OPEN CONFIG FALID/n");
  18.         return 0;
  19.     }
  20.     fseek(f,0,SEEK_SET); 
  21.     while(fgets(config_linebuf,256,f) != NULL)
  22.     {   
  23.         if(strlen(config_linebuf) < 3) //判断是否是空行
  24.         {
  25.             continue;
  26.         }
  27.         if (config_linebuf[strlen(config_linebuf)-1] == 10) //去除最后一位是/n的情况
  28.         {
  29.             
  30.             memset(exchange_buf,0,sizeof(exchange_buf));
  31.             strncpy(exchange_buf,config_linebuf,strlen(config_linebuf)-1);
  32.             memset(config_linebuf,0,sizeof(config_linebuf));
  33.             strcpy(config_linebuf,exchange_buf);
  34.         }
  35.         memset(line_name,0,sizeof(line_name));
  36.         leave_line = strstr(config_linebuf,config_sign);
  37.         if(leave_line == NULL)                            //去除无"="的情况
  38.         {
  39.             continue;
  40.         }
  41.         int leave_num = leave_line - config_linebuf;
  42.         strncpy(line_name,config_linebuf,leave_num);
  43.         if(strcmp(line_name,conf_name) ==0)
  44.         {
  45.             strncpy(config_buff,config_linebuf+(leave_num+1),strlen(config_linebuf)-leave_num-1);
                break;
  46.         }
  47.         if(fgetc(f)==EOF)
  48.         {
  49.             break;  
  50.         }
  51.         fseek(f,-1,SEEK_CUR);
  52.         memset(config_linebuf,0,sizeof(config_linebuf));
  53.     }
  54.     fclose(f);
  55. }


  56. /*
  57.  *添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加)
  58.  *
  59.  *输入参数:1,配置文件路径 2,匹配标记 3,替换或添加的内容
  60.  *
  61.  */
  62. AddOrAltConfig(char *conf_path,char *conf_name,char *config_buff)
  63. {
  64.     
  65.     char config_linebuf[256];
  66.     char line_name[40];
  67.     char *config_sign = "=";
  68.     char *leave_line;
  69.     int  alter_sign = 0;
  70.     
  71.     FILE *f;
  72.     f = fopen(conf_path,"r+");
  73.     if(f == NULL)
  74.     {
  75.         printf("OPEN CONFIG FALID/n");
  76.         return 0;
  77.     }
  78.     fseek(f,0,SEEK_END);
  79.     long congig_lenth = ftell(f);
  80.     int configbuf_lenth = strlen(config_buff);
  81.     configbuf_lenth = configbuf_lenth + 5;
  82.     char sum_buf[congig_lenth+configbuf_lenth];
  83.     memset(sum_buf,0,sizeof(sum_buf));
  84.     fseek(f,0,SEEK_SET); 
  85.     while(fgets(config_linebuf,256,f) != NULL)
  86.     {   
  87.         if(strlen(config_linebuf) < 3) //判断是否是空行
  88.         {
  89.             strcat(sum_buf,config_linebuf);
  90.             continue;
  91.         }
  92.         leave_line = NULL;
  93.         leave_line = strstr(config_linebuf,config_sign);
  94.         if(leave_line == NULL)                            //去除无"="的情况
  95.         {
  96.             strcat(sum_buf,config_linebuf);
  97.             continue;
  98.         }
  99.         int leave_num = leave_line - config_linebuf;
  100.         memset(line_name,0,sizeof(line_name));
  101.         strncpy(line_name,config_linebuf,leave_num);
  102.         if(strcmp(line_name,conf_name) ==0)
  103.         {
  104.             strcat(sum_buf,config_buff);
  105.             strcat(sum_buf,"/n");
  106.             alter_sign = 1;
  107.         }
  108.         else
  109.         {
  110.             strcat(sum_buf,config_linebuf);
  111.         }
  112.         if(fgetc(f)==EOF)
  113.         {
  114.             break;  
  115.         }
  116.         fseek(f,-1,SEEK_CUR);
  117.         memset(config_linebuf,0,sizeof(config_linebuf));
  118.     }
  119.     if(alter_sign == 0)
  120.     {
  121.         strcat(sum_buf,config_buff);
  122.         strcat(sum_buf,"/n");
  123.     }
  124.     printf("---sum_buf---->%s<----------/n",sum_buf);
  125.     remove(conf_path);
  126.     fclose(f);
  127.     FILE *fp;
  128.     fp = fopen(conf_path,"w+");
  129.     if(fp == NULL)
  130.     {
  131.         printf("OPEN CONFIG FALID/n");
  132.         return 2;
  133.     }
  134.     fseek(fp,0,SEEK_SET);
  135.     fputs(sum_buf,fp);
  136.     fclose(fp);
  137. }



  138. /*
  139.  *删除配置文件内容(
  140.  *
  141.  *输入参数:1,配置文件路径 2,匹配标记 
  142.  *
  143.  */
  144. DeleteConfig(char *conf_path,char *conf_name)
  145. {
  146.     
  147.     char config_linebuf[256];
  148.     char line_name[40];
  149.     char *config_sign = "=";
  150.     char *leave_line;
  151.     
  152.     FILE *f;
  153.     f = fopen(conf_path,"r+");
  154.     if(f == NULL)
  155.     {
  156.         printf("OPEN CONFIG FALID/n");
  157.         return 0;
  158.     }
  159.     fseek(f,0,SEEK_END);
  160.     long congig_lenth = ftell(f);
  161.     char sum_buf[congig_lenth+2];
  162.     memset(sum_buf,0,sizeof(sum_buf));
  163.     fseek(f,0,SEEK_SET); 
  164.     while(fgets(config_linebuf,256,f) != NULL)
  165.     {   
  166.         if(strlen(config_linebuf) < 3) //判断是否是空行
  167.         {
  168.             strcat(sum_buf,config_linebuf);
  169.             continue;
  170.         }
  171.         leave_line = NULL;
  172.         leave_line = strstr(config_linebuf,config_sign);
  173.         if(leave_line == NULL)                            //去除无"="的情况
  174.         {
  175.             strcat(sum_buf,config_linebuf);
  176.             continue;
  177.         }
  178.         int leave_num = leave_line - config_linebuf;
  179.         memset(line_name,0,sizeof(line_name));
  180.         strncpy(line_name,config_linebuf,leave_num);
  181.         if(strcmp(line_name,conf_name) ==0)
  182.         {
  183.             
  184.         }
  185.         else
  186.         {
  187.             strcat(sum_buf,config_linebuf);
  188.         }
  189.         
  190.         if(fgetc(f)==EOF)
  191.         {
  192.             break;  
  193.         }
  194.         fseek(f,-1,SEEK_CUR);
  195.         memset(config_linebuf,0,sizeof(config_linebuf));
  196.     }
  197.     printf("---sum_buf---->%s<----------/n",sum_buf);
  198.     remove(conf_path);
  199.     fclose(f);
  200.     FILE *fp;
  201.     fp = fopen(conf_path,"w+");
  202.     if(fp == NULL)
  203.     {
  204.         printf("OPEN CONFIG FALID/n");
  205.         return 2;
  206.     }
  207.     fseek(fp,0,SEEK_SET);
  208.     fputs(sum_buf,fp);
  209.     fclose(fp);
  210. }

你可能感兴趣的:(Posix)