欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言读取配置文件

程序员文章站 2022-03-23 19:53:19
c语言读取配置文件,配置文件的格式如下: key1 = value1 key2 = value2 . 名值对以一个=链接,一条记录以换行符分割 头文件: #include #include...

c语言读取配置文件,配置文件的格式如下:

key1 = value1

key2 = value2 .

名值对以一个=链接,一条记录以换行符分割

头文件:

#include
#include
#include 

函数原型:

void trim(char *strin, char *strout);//去除字符串首位空格

void getvalue(char * keyandvalue, char * key, char * value);
//根据key得到value

int writecfg(const char *filename/*in*/, const char *key/*in*/, const char *value/*in*/);
//写入配置文件

void readcfg(const char *filename/*in*/, const char *key/*in*/, const char **value/*out*/);
//读取配置文件

函数实现:

  1 void trim(char *strin, char *strout){
  2 
  3     char *start, *end, *temp;//定义去除空格后字符串的头尾指针和遍历指针
  4 
  5     temp = strin;
  6 
  7     while (*temp == ' '){
  8         ++temp;
  9     }
 10 
 11     start = temp; //求得头指针
 12 
 13     temp = strin + strlen(strin) - 1; //得到原字符串最后一个字符的指针(不是'\0')
 14 
 15     while (*temp == ' '){
 16         --temp;
 17     }
 18 
 19     end = temp; //求得尾指针
 20 
 21 
 22     for(strin = start; strin <= end; ){
 23         *strout++ = *strin++;
 24     }
 25 
 26     *strout = '\0';
 27 }
 28 
 29 void getvalue(char * keyandvalue, char * key, char * value){
 30 
 31     char *p = keyandvalue;
 32 
 33     p = strstr(keyandvalue, key);
 34     if(p == null){
 35         //printf("没有key\n");
 36         return ;
 37     }
 38 
 39     p += strlen(key);
 40     trim(p, value);
 41 
 42     p = strstr(value, "=");
 43     if(p == null){
 44         printf("没有=\n");
 45         return;
 46     }
 47     p+= strlen("=");
 48     trim(p, value);
 49 
 50     p = strstr(value, "=");
 51     if(p != null){
 52         printf("多余的=\n");
 53         return;
 54     }
 55     p = value;
 56     trim(p, value);
 57 
 58 }
 59 int writecfg(const char *filename/*in*/, const char *key/*in*/, const char *value/*in*/){
 60 
 61     file *pf = null;
 62     char ftemp[flen] = {0}, fline[1024] = {0}, *fp;    //文件缓存数组
 63     long fsize = 0;
 64     int reg = 0;
 65     int exit = 0;
 66     int i = 0;
 67 
 68     pf = fopen(filename, "r+");
 69     if(pf == null){
 70         pf = fopen(filename, "w+");
 71     }
 72     //获得文件大小
 73     fseek(pf, 0, seek_end); // 将文件指针指向末尾
 74     fsize = ftell(pf);
 75     if(fsize > flen){
 76         printf("文件不能超过8k\n");
 77         reg = -1;
 78         goto end;
 79     }
 80     fseek(pf, 0, seek_set); //将文件指针指向开头
 81 
 82     //一行一行的读,如果存在key则修改value存到缓存数组中
 83     while(!feof(pf)){
 84         fgets(fline, 1024, pf);
 85         if(strstr(fline, key) != null && exit == 1)
 86             strcpy(fline, "");
 87         if(strstr(fline, key) != null && exit == 0){ //判断key是否存在
 88             exit = 1;
 89             sprintf(fline,"%s = %s\n", key, value);
 90         }
 91         
 92         printf("fline = %s\n", fline);
 93         strcat(ftemp, fline);
 94         
 95     }
 96     if(exit != 1){//如果不存在则把key value写入到最后一行
 97         sprintf(fline,"%s = %s\n", key, value);
 98         strcat(ftemp, fline);
 99     }
100     if(pf != null){
101         fclose(pf);
102         pf = fopen(filename, "w+");
103         fp = (char *)malloc(sizeof(char) * strlen(ftemp) + 1);
104         strcpy(fp, ftemp);
105         fp[strlen(fp) - 1] = eof;
106         fputs(fp, pf);
107         if(fp != null){
108             free(fp);
109             fp = null;
110         }
111         fclose(pf);
112     }
113     end :
114         if(pf != null)
115             fclose(pf);
116     //重新创建一个以filename命名的文件
117     return reg;
118 }
119 
120 void readcfg(const char *filename/*in*/, const char *key/*in*/, const char **value/*out*/){
121 
122     file *pf = null;
123     char line[1024] = {0}, vtemp[1024] = {0};
124 
125     pf = fopen(filename, "r"); //以只读方式打开
126     
127     while(!feof(pf)){
128         fgets(line, 1024, pf);
129         getvalue(line, key, vtemp);
130         if(strlen(vtemp) != 0)
131             break;
132     }
133     if(strlen(vtemp) != 0){
134         *value = (char *)malloc(sizeof(char) * strlen(vtemp) + 1);
135         strcpy(*value, vtemp);
136     }    
137     else
138         *value = null;
139     if(pf != null)
140         fclose(pf);
141 }

测试:

 1 #define filename "c:/cfg.ini"
 2 void menu(){
 3     printf("===========================\n");
 4     printf("1 写入配置文件\n");
 5     printf("2 读取配置文件\n");
 6     printf("0 退出程序");
 7     printf("===========================\n");
 8 }
 9 
10 int twrite(){
11     
12     char key[1024] = {0}, value[1024] = {0};
13 
14     printf("请输入key:");
15     scanf("%s", key);
16     printf("请输入value:");
17     scanf("%s", value);
18     printf("\n您输入的是:%s = %s\n", key, value);
19 
20     return writecfg(filename/*in*/,key/*in*/,value/*in*/);
21 }
22 
23 void tread(){
24     char key[1024] = {0}, *value;
25 
26     printf("请输入key:");
27     scanf("%s", key);
28 
29     readcfg(filename/*in*/,key/*in*/, &value/*out*/);
30     if(value == null){
31         printf("没有key\n");
32         return ;
33     }
34     printf("\nvalue = %s\n", value);
35 
36     if(value != null){
37         free(value);
38         value = null;
39     }
40 
41 }
42 int main(){
43     
44     int choose;
45     
46     while(1){
47         choose = 0;
48         menu();
49         printf("请输入选择:");
50         scanf("%d", &choose);
51         switch(choose){
52             case 1:
53                 if(twrite() == -1)
54                     return -1;
55                 break;
56             case 2:
57                 tread();
58                 break;
59             case 0:
60                 return 0;
61             default: 
62                 return 0;
63         }
64     }
65     system("pause");
66     return 0;
67 }

运行结果:

C语言读取配置文件