linux读取按行读写文本文件

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <string.h>
  5. #include <malloc.h>
  6. #include <stdlib.h> 


  7. typedef struct item_t {
  8.     char *key;
  9.     char *value;
  10. }ITEM;

  11. /*
  12.  *去除字符串右端空格
  13.  */
  14. char *strtrimr(char *pstr)
  15. {
  16.     int i;
  17.     i = strlen(pstr) - 1;
  18.     while (isspace(pstr[i]) && (>= 0))
  19.         pstr[i--] = '\0';
  20.     return pstr;
  21. }
  22. /*
  23.  *去除字符串左端空格
  24.  */
  25. char *strtriml(char *pstr)
  26. {
  27.     int i = 0,j;
  28.     j = strlen(pstr) - 1;
  29.     while (isspace(pstr[i]) && (<= j))
  30.         i++;
  31.     if (0<i)
  32.         strcpy(pstr, &pstr[i]);
  33.     return pstr;
  34. }
  35. /*
  36.  *去除字符串两端空格
  37.  */
  38. char *strtrim(char *pstr)
  39. {
  40.     char *p;
  41.     p = strtrimr(pstr);
  42.     return strtriml(p);
  43. }


  44. /*
  45.  *从配置文件的一行读出key或value,返回item指针
  46.  *line--从配置文件读出的一行
  47.  */
  48. int get_item_from_line(char *line, struct item_t *item)
  49. {
  50.     char *= strtrim(line);
  51.     int len = strlen(p);
  52.     if(len <= 0){
  53.         return 1;//空行
  54.     }
  55.     else if(p[0]=='#'){
  56.         return 2;
  57.     }else{
  58.         char *p2 = strchr(p, '=');
  59.         *p2++ = '\0';
  60.         item->key = (char *)malloc(strlen(p) + 1);
  61.         item->value = (char *)malloc(strlen(p2) + 1);
  62.         strcpy(item->key,p);
  63.         strcpy(item->value,p2);

  64.         }
  65.     return 0;//查询成功
  66. }

  67. int file_to_items(const char *file, struct item_t *items, int *num)
  68. {
  69.     char line[1024];
  70.     FILE *fp;
  71.     fp = fopen(file,"r");
  72.     if(fp == NULL)
  73.         return 1;
  74.     int i = 0;
  75.     while(fgets(line, 1023, fp))
  76.     {
  77.         char *= strtrim(line);
  78.         int len = strlen(p);
  79.         if(len <= 0)
  80.         {
  81.             continue;
  82.         }
  83.         else if(p[0]=='#')
  84.         {
  85.             continue;
  86.         }
  87.         else
  88.         {
  89.             char *p2 = strchr(p, '=');
  90.             /*这里认为只有key没什么意义*/
  91.             if(p2 == NULL)
  92.                 continue;
  93.             *p2++ = '\0';
  94.             items[i].key = (char *)malloc(strlen(p) + 1);
  95.             items[i].value = (char *)malloc(strlen(p2) + 1);
  96.             strcpy(items[i].key,p);
  97.             strcpy(items[i].value,p2);

  98.             i++;
  99.         }
  100.     }
  101.     (*num) = i;
  102.     fclose(fp);
  103.     return 0;
  104. }

  105. /*
  106.  *读取value
  107.  */
  108. int read_conf_value(const char *key,char *value1,const char *file)
  109. {
  110.     char line[1024];
  111.     char *key1,*key3,*key2;
  112.     FILE *fp;
  113.     fp = fopen(file,"r");
  114.     if(fp == NULL)
  115.         return 1;//文件打开错误
  116.     while (fgets(line, 1023, fp)){
  117.         ITEM item;
  118.         get_item_from_line(line,&item);
  119.         if(!strcmp(item.key,key)){

  120.             strcpy(value1,item.value);
  121.             fclose(fp);
  122.             free(item.key);
  123.             free(item.value);
  124.             break;
  125.         }
  126.     }
  127.     return 0;//成功

  128. }
  129. int write_conf_value(const char *key,char *value,const char *file)
  130. {
  131.     ITEM items[20];// 假定配置项最多有20个
  132.     int num;//存储从文件读取的有效数目
  133.     file_to_items(file, items, &num);

  134.     int i=0;
  135.     //查找要修改的项
  136.     for(i=0;i<num;i++){
  137.         if(!strcmp(items[i].key, key)){
  138.             items[i].value = value;
  139.             break;
  140.         }
  141.     }

  142.     // 更新配置文件,应该有备份,下面的操作会将文件内容清除
  143.     FILE *fp;
  144.     fp = fopen(file, "w");
  145.     if(fp == NULL)
  146.         return 1;

  147.     i=0;
  148.     for(i=0;i<num;i++){
  149.         fprintf(fp,"%s=%s\n",items[i].key, items[i].value);
  150.         //printf("%s=%s\n",items[i].key, items[i].value);
  151.     }
  152.     fclose(fp);
  153.     //清除工作
  154.  /* i=0;
  155.     for(i=0;i<num;i++){
  156.         free(items[i].key);
  157.         free(items[i].value);
  158.     }*/

  159.     return 0;

  160. }

  161. void main(void)
  162. {
  163.     char *key;
  164.     char *value=NULL,*value1=NULL;
  165.     char *file;
  166.     file="/home/wangwei/ww/file/from_file";

  167.     key="IP";
  168.     value=(char *)malloc(sizeof(char)*30);
  169.     value1=(char *)malloc(sizeof(char)*30);
  170.     read_conf_value(key,value,file);
  171.     printf("IP = %s\n",value);

  172.     key="MASK";
  173.     read_conf_value(key,value,file);
  174.     printf("MASK = %s\n",value);

  175.     key="GATEWAY";
  176.     read_conf_value(key,value,file);
  177.     printf("GATEWAY = %s\n",value);
  178.     free(value);
  179.     free(value1);
  180.     value=NULL;
  181.     value1=NULL;
  182. }

FROM:  http://blog.chinaunix.net/uid-26921272-id-3328858.html







  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值