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.
 
8.
 
9.typedef struct  item_t {
 
10.    char  *key;
 
11.    char  *value;
 
12.}ITEM;
 
13.
 
14. /*
 
15. *去除字符串右端空格
 
16. */
 
17. char  *strtrimr( char  *pstr)
 
18.{
 
19.    int  i;
 
20.    i = strlen(pstr) - 1;
 
21.    while  (isspace(pstr[i]) && (i >= 0))
 
22.        pstr[i--] = '\0' ;
 
23.    return  pstr;
 
24.}
 
25. /*
 
26. *去除字符串左端空格
 
27. */
 
28. char  *strtriml( char  *pstr)
 
29.{
 
30.    int  i = 0,j;
 
31.    j = strlen(pstr) - 1;
 
32.    while  (isspace(pstr[i]) && (i <= j))
 
33.        i++;
 
34.    if  (0<i)
 
35.        strcpy(pstr, &pstr[i]);
 
36.    return  pstr;
 
37.}
 
38. /*
 
39. *去除字符串两端空格
 
40. */
 
41. char  *strtrim( char  *pstr)
 
42.{
 
43.    char  *p;
 
44.    p = strtrimr(pstr);
 
45.    return  strtriml(p);
 
46.}
 
47.
 
48.
 
49. /*
 
50. *从配置文件的一行读出key或value,返回item指针
 
51. *line--从配置文件读出的一行
 
52. */
 
53. int  get_item_from_line( char  *line, struct  item_t *item)
 
54.{
 
55.    char  *p = strtrim(line);
 
56.    int  len = strlen(p);
 
57.    if (len <= 0){
 
58.        return  1; //空行
 
59.    }
 
60.    else  if (p[0]== '#' ){
 
61.        return  2;
 
62.    } else {
 
63.        char  *p2 = strchr(p, '=' );
 
64.        *p2++ = '\0' ;
 
65.        item->key = ( char  *)malloc(strlen(p) + 1);
 
66.        item->value = ( char  *)malloc(strlen(p2) + 1);
 
67.        strcpy(item->key,p);
 
68.        strcpy(item->value,p2);
 
69.
 
70.        }
 
71.    return  0; //查询成功
 
72.}
 
73.
 
74. int  file_to_items( const  char  *file, struct  item_t *items, int  *num)
 
75.{
 
76.    char  line[1024];
 
77.    FILE *fp;
 
78.    fp = fopen(file, "r" );
 
79.    if (fp == NULL)
 
80.        return  1;
 
81.    int  i = 0;
 
82.    while (fgets(line, 1023, fp))
 
83.    {
 
84.        char  *p = strtrim(line);
 
85.        int  len = strlen(p);
 
86.        if (len <= 0)
 
87.        {
 
88.            continue ;
 
89.        }
 
90.        else  if (p[0]== '#' )
 
91.        {
 
92.            continue ;
 
93.        }
 
94.        else
 
95.        {
 
96.            char  *p2 = strchr(p, '=' );
 
97.            /*这里认为只有key没什么意义*/
 
98.            if (p2 == NULL)
 
99.                continue ;
 
100.            *p2++ = '\0' ;
 
101.            items[i].key = ( char  *)malloc(strlen(p) + 1);
 
102.            items[i].value = ( char  *)malloc(strlen(p2) + 1);
 
103.            strcpy(items[i].key,p);
 
104.            strcpy(items[i].value,p2);
 
105.
 
106.            i++;
 
107.        }
 
108.    }
 
109.    (*num) = i;
 
110.    fclose(fp);
 
111.    return  0;
 
112.}
 
113.
 
114. /*
 
115. *读取value
 
116. */
 
117. int  read_conf_value( const  char  *key, char  *value1, const  char  *file)
 
118.{
 
119.    char  line[1024];
 
120.    char  *key1,*key3,*key2;
 
121.    FILE *fp;
 
122.    fp = fopen(file, "r" );
 
123.    if (fp == NULL)
 
124.        return  1; //文件打开错误
 
125.    while  (fgets(line, 1023, fp)){
 
126.        ITEM item;
 
127.        get_item_from_line(line,&item);
 
128.        if (!strcmp(item.key,key)){
 
129.
 
130.            strcpy(value1,item.value);
 
131.            fclose(fp);
 
132.            free(item.key);
 
133.            free(item.value);
 
134.            break ;
 
135.        }
 
136.    }
 
137.    return  0; //成功
 
138.
 
139.}
 
140. int  write_conf_value( const  char  *key, char  *value, const  char  *file)
 
141.{
 
142.    ITEM items[20]; // 假定配置项最多有20个
 
143.    int  num; //存储从文件读取的有效数目
 
144.    file_to_items(file, items, &num);
 
145.
 
146.    int  i=0;
 
147.    //查找要修改的项
 
148.    for (i=0;i<num;i++){
 
149.        if (!strcmp(items[i].key, key)){
 
150.            items[i].value = value;
 
151.            break ;
 
152.        }
 
153.    }
 
154.
 
155.    // 更新配置文件,应该有备份,下面的操作会将文件内容清除
 
156.    FILE *fp;
 
157.    fp = fopen(file, "w" );
 
158.    if (fp == NULL)
 
159.        return  1;
 
160.
 
161.    i=0;
 
162.    for (i=0;i<num;i++){
 
163.        fprintf(fp, "%s=%s\n" ,items[i].key, items[i].value);
 
164.        //printf("%s=%s\n",items[i].key, items[i].value);
 
165.    }
 
166.    fclose(fp);
 
167.    //清除工作
 
168. /*i=0;
 
169.    for(i=0;i<num;i++){
 
170.        free(items[i].key);
 
171.        free(items[i].value);
 
172.    }*/
 
173.
 
174.    return  0;
 
175.
 
176.}
 
177.
 
178. void  main( void )
 
179.{
 
180.    char  *key;
 
181.    char  *value=NULL,*value1=NULL;
 
182.    char  *file;
 
183.    file= "/home/wangwei/ww/file/from_file" ;
 
184.
 
185.    key= "IP" ;
 
186.    value=( char  *)malloc( sizeof ( char )*30);
 
187.    value1=( char  *)malloc( sizeof ( char )*30);
 
188.    read_conf_value(key,value,file);
 
189.    printf( "IP = %s\n" ,value);
 
190.
 
191.    key= "MASK" ;
 
192.    read_conf_value(key,value,file);
 
193.    printf( "MASK = %s\n" ,value);
 
194.
 
195.    key= "GATEWAY" ;
 
196.    read_conf_value(key,value,file);
 
197.    printf( "GATEWAY = %s\n" ,value);
 
198.    free(value);
 
199.    free(value1);
 
200.    value=NULL;
 
201.    value1=NULL;
 
202.}

  

本文转自夜&枫博客园博客,原文链接:http://www.cnblogs.com/newstart/archive/2013/05/09/3069499.html,如需转载请自行联系原作者
相关文章
|
17天前
|
Linux Shell
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
70 1
|
21天前
|
Linux 数据安全/隐私保护 Windows
Linux入门指南:linux权限究竟是什么?和文件有什么关系?
Linux入门指南:linux权限究竟是什么?和文件有什么关系?
30 0
|
23天前
|
算法 Linux C++
【Linux系统编程】解析获取和设置文件信息与权限的Linux系统调用
【Linux系统编程】解析获取和设置文件信息与权限的Linux系统调用
29 0
|
27天前
|
Shell Linux API
【Shell 命令集合 备份压缩 】Linux 解压缩文件 unzip命令 使用指南
【Shell 命令集合 备份压缩 】Linux 解压缩文件 unzip命令 使用指南
49 0
|
16天前
|
人工智能 安全 Linux
【Linux】Linux之间如何互传文件(详细讲解)
【Linux】Linux之间如何互传文件(详细讲解)
|
27天前
|
Shell Linux C语言
【Shell 命令集合 系统设置 】Linux 创建Kickstart文件mkkickstart命令 使用指南
【Shell 命令集合 系统设置 】Linux 创建Kickstart文件mkkickstart命令 使用指南
31 0
|
27天前
|
存储 Shell Linux
【Shell 命令集合 备份压缩 】Linux 解码uuencode编码的文件 uudecode 命令 使用指南
【Shell 命令集合 备份压缩 】Linux 解码uuencode编码的文件 uudecode 命令 使用指南
29 0
|
27天前
|
安全 Shell Linux
【Shell 命令集合 备份压缩 】Linux将可执行文件压缩成gzip格式 gzexe命令 使用指南
【Shell 命令集合 备份压缩 】Linux将可执行文件压缩成gzip格式 gzexe命令 使用指南
35 0
|
27天前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
48 0
|
27天前
|
存储 Shell Linux
【Shell 命令集合 备份压缩 】Linux 恢复由dump命令创建的备份文件 restore命令 使用指南
【Shell 命令集合 备份压缩 】Linux 恢复由dump命令创建的备份文件 restore命令 使用指南
31 0