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,如需转载请自行联系原作者