C语言的文件读写小案例

C语言一个读写配置的简单案例

FILE1:

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
#ifndef _CONFIG_H
#define _CONFIG_H


struct ConfigInfo{
char key[64];
char value[64];
};


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int getConfigFileLines(char *path);

int isValidLine(char *string);

void fileResolving(char * filePath, int lines, struct ConfigInfo ** config);

char *getValueByKey(char *key, struct ConfigInfo *config, int lines);

void freeConfigInfo(struct ConfigInfo *configinfo);

#endif //_CONFIG_H

FILE2:

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
#include "config.h"


int getConfigFileLines(char *ConfigPath) {

FILE *configFile = fopen(ConfigPath, "r");

if (configFile == NULL) {
return -1;
}

char tmpBuf[1024] = {0};
int configLine = 0;
while (fgets(tmpBuf, 1024, configFile) != NULL) {

// printf("000+");

if (isValidLine(tmpBuf)) {
configLine++;
memset(tmpBuf, 0, 1024);
}

}

fclose(configFile);

return configLine;


}

int isValidLine(char *string) {

if (strchr(string, ':') == NULL) {

return 0;

}


return 1;
}


void fileResolving(char *filePath, int lines, struct ConfigInfo **config) {

struct ConfigInfo *configInfo = malloc(sizeof(struct ConfigInfo) * lines);

if (configInfo == NULL) {
return;
}

FILE *file = fopen(filePath, "r");

if (file == NULL) {
perror("Failed");

}


char key[64] = {0};
char value[64] = {0};

char tmpBuf[1024] = {0};
int configNum = 0;
while (fgets(tmpBuf, 1024, file) != NULL) {

if (isValidLine(tmpBuf)) {
memset(configInfo[configNum].key, 0, 1024);
memset(configInfo[configNum].value, 0, 1024);


char *pos = strchr(tmpBuf, ':');
strncpy(configInfo[configNum].key, tmpBuf, pos - tmpBuf);
strncpy(configInfo[configNum].value, pos + 1, strlen(pos + 1) - 1);

printf("0000000000:%s\n", configInfo[configNum].key);
printf("1111111111:%s\n", configInfo[configNum].value);

configNum++;
}

memset(tmpBuf, 0, 1024);

}

*config = configInfo;

}

char *getValueByKey(char *key, struct ConfigInfo *config, int lines) {

printf("aaa");

if (key == NULL || lines == 0) {

perror("aaa");
return NULL;
}

for (int i = 0; i < lines; i++) {
if (strcmp(key, config[i].key) == 0) {
return config[i].value;
}
}


return NULL;


void freeConfigInfo(struct ConfigInfo *configinfo) {
if (configinfo != NULL) {
free(configinfo);
configinfo = NULL;
}
}

FILE3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include "config.h"

int main(int argc, char *argv[]) {

char *configPath = "config.ini";

int lines = getConfigFileLines(configPath);

printf("%d\n", lines);

struct ConfigInfo *configInfo = NULL;

fileResolving(configPath, lines, &configInfo);


printf("%s\n", getValueByKey("heroId", configInfo, lines));


freeConfigInfo(configInfo);

return 0;
}