最新消息:

有用的代码小片段4-C实现修改配置文件

C/C++ admin 3132浏览 0评论

如果配置文件时这样的:
#this is a simple configure file
myname=yuqiang
age=23
..
..
..

那面下面两个函数应该操作的不错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
 
/*
*   getConf: get a config value from a configfile.
*
*   args:
*       confname:   config name
*       value:      the value of config name
*
*   return:
*       0:  success
*       -1: failed
*
*/
int getConf(const char *configfile, const char *confname, char *value)
{
    int i;
    char line[128]={0};
    char conbuff[128]={0};
    int math=0;
    FILE *f=fopen(configfile,"r");
    if(f==NULL) {
        show_debug_info( 1,"[error] open config file %s",configfile);
        return -1;
    }
    while( fgets(line,sizeof(line),f) ) {
        if( *line=='#' || *line=='n' ) {
            memset(line,0,sizeof(line));
            continue;
        }
        memset(conbuff,0,sizeof(conbuff));
        i=0;
        while( line[i]!='=' && i<strlen(line)-1 ) {
            conbuff[i]=line[i];
            i++;
        }
        if( i==strlen(line) ) {
            memset(line,0,sizeof(line));
            continue;
        }
        if( strcmp(conbuff,confname) ) {
            memset(line,0,sizeof(line));
            continue;
        } else {
            if( strlen(confname)+2==strlen(line) )
                return -1;
            math=1;
            memset(value,0,strlen(value));
            memcpy(value,line+strlen(confname)+1,strlen(line)-strlen(confname)-2);//还有一个'n'
            //printf("|%s|=|%s|n",confname,value);
            break;
        }
        memset(line,0,sizeof(line));
    }
    fclose(f);
 
    if(math)
        return 0;
    else
        return -1;
}
 
/*
*   setConf: set a config value to a configfile.
*
*   args:
*       confname:   config name
*       value:      the value of config name
*
*   return :
*       0:  success
*       1:  add a new config
*       -1: failed
*
*/
int setConf(const char *configfile, const char *confname, const char *value)
{
    char conbuff[128]={0};
    int i;
    char line[128]={0};
    int math=0;
    FILE *f=fopen(configfile,"r");
    if(f==NULL) {
        FILE *newf=fopen(configfile,"a");
        if(newf==NULL) {
            show_debug_info( 1,"[error] open config file %s",configfile);
            return -1;
        }
        sprintf(conbuff,"%s=%sn",confname,value);
        fputs(conbuff,newf);
        fclose(newf);
        return 1;
    }
    /* open a tmp file to save config */
    FILE *tmp=tmpfile();
    if(tmp==NULL) {
        return -1;
    }
 
    /*parse config file and save to tmp file*/
    while( fgets(line,sizeof(line),f) ) {
        memset(conbuff,0,sizeof(conbuff));
        if( *line=='#' || *line=='n' ) {
            fputs(line,tmp);
            memset(line,0,sizeof(line));
            continue;
        }
        i=0;
        while( line[i]!='=' && i<strlen(line) ) {
            conbuff[i]=line[i];
            i++;
        }
        if( strcmp(conbuff,confname) ) {
            fputs(line,tmp);
        }
        else {
            math=1;
            memset(conbuff,0,sizeof(conbuff));
            sprintf(conbuff,"%s=%sn",confname,value);
            fputs(conbuff,tmp);
        }
        memset(line,0,sizeof(line));
    }
    if(math==0) {
        memset(conbuff,0,sizeof(conbuff));
        sprintf(conbuff,"%s=%sn",confname,value);
        fputs(conbuff,tmp);
    }
    fclose(f);
 
    fflush(tmp);
    rewind(tmp);
 
    /*copy tmp file's data to config file*/
    f=fopen(configfile,"w+");
    if(f==NULL) {
        show_debug_info( 1,"[error] open config file %s",configfile);
        return -1;
    }
 
    memset(line,0,sizeof(line));
    while( fgets(line,sizeof(line),tmp) ) {
        fputs(line,f);
        memset(line,0,sizeof(line));
    }
 
    fclose(tmp);
    fclose(f);
    if(math)
        return 0;
    else
        return 1;
}

转载请注明:爱开源 » 有用的代码小片段4-C实现修改配置文件

您必须 登录 才能发表评论!