API函数配置读写文件

cfg_op.h

1
2
3
4
5
6
7
8
9
10
11
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
//获取配置项
int GetCfgItem(char *pFileName/*in*/, char*pKey/*in*/, char *pValue/*in out*/, int *pValueLen/*out*/);
//写配置项
int WriteCfgItem(char*pFileName/*in*/, char *pKey/*in*/, char *pValue/*in*/, int *ValueLen/*in*/);
#ifdef __cplusplus
}
#endif

cfg_op.c

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
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#define Maxline 2048
extern "C" {
int GetCfgItem(char *pFileName, char*pKey, char *pValue, int *pValueLen)
{
int ret = 0;
FILE *fp = NULL;
char lineBuf[Maxline];
char *pTmp = NULL, *pEnd = NULL, *pBegin = NULL;
fp = fopen(pFileName, "r");
if (fp == NULL) {
ret = -1;
return ret;
}
while (!feof(fp)) {
memset(lineBuf, 0, sizeof(lineBuf));
fgets(lineBuf, Maxline, fp);
printf("lineBuf:%s ", lineBuf);

pTmp = strchr(lineBuf, '=');
if (pTmp == NULL) { //没有=号
continue;
}
pTmp = strstr(lineBuf, pKey);
if (pTmp == NULL) { //判断key是不是在//所在行 是不是有key
continue;
}
pTmp = pTmp + strlen(pKey); // mykey1 = myvalue11111111==>"myvalue11111111"
pTmp = strchr(pTmp, '=');
if (pTmp == NULL) { //判断所在行是不是有key
continue;
}
pTmp++;
printf("pTmp:%s ", pTmp);
//获取value起点
while (1) {
if (*pTmp == ' ') {
pTmp++;
}
else {
pBegin = pTmp;
if (*pBegin == '\n') {
//没有配置value
printf("配置项:%s 没有配置value \n", pKey);
goto End;
}
break;
}
}

//获取value结束点
while (1) {
if (*pTmp == ' ' || *pTmp == '\n') {
break;
}
else {
pTmp++;
}
}
pEnd = pTmp;
//赋值
*pValueLen = pEnd - pBegin;
memcpy(pValue, pBegin, pEnd - pBegin);
}
End:
if (fp == NULL)
fclose(fp);
return ret;
}
int WriteCfgItem(char*pFileName, char *pKey, char *pValue, int *ValueLen)
{
int rv = 0, iTag = 0, length = 0;
FILE *fp = NULL;
char lineBuf[Maxline];
char *pTmp = NULL, *pBegin = NULL, *pEnd = NULL;
char filebuf[1024 * 8] = { 0 };
if (pFileName == NULL || pKey == NULL || pValue == NULL) {
rv = -1;
printf("SetCfgItem() err. param err \n");
goto End;
}
fp = fopen(pFileName, "r+");
if (fp == NULL) {
rv = -2;
printf("fopen() err.\n");
}
if (fp == NULL) {
fp = fopen(pFileName, "w+t");
if (fp == NULL) {
rv = -3;
printf("fopen() err.\n");
goto End;
}
}
fseek(fp, 0L, SEEK_END);//把文件指针从0位置开始,移动到文件末尾
//获取文件长度
length = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if (length > 1024 * 8) {
rv = -3;
printf("文件超过1024*8,nunsupport");
goto End;
}
while (!feof(fp)) {
//读每一行
memset(lineBuf, 0, sizeof(lineBuf));
pTmp = fgets(lineBuf, Maxline, fp);
if (pTmp == NULL) {
break;
}
//key关键字是否在本行
pTmp = strstr(lineBuf, pKey);
if (pTmp == NULL)//key关键字不在本行,copy到filebuf中
{
strcat(filebuf, lineBuf);
continue;
}
else//key关键字在本行中,替换旧的行,再copy到filebuf中
{
sprintf(lineBuf, "%s = %s\n", pKey, pValue);
strcat(filebuf, lineBuf);
//若存在key
iTag = 1;
}
}
//若key关键字,不存在 追加
if (iTag == 0) {
fprintf(fp, "%s = %s\n", pKey, pValue);
}
else {//若key关键字,存在,则重新创建文件
if (fp != NULL) {
fclose(fp);
fp = NULL;//避免野指针
}
fp = fopen(pFileName, "w+t");
if (fp == NULL) {
rv = -4;
printf("fopen() err.\n");
goto End;
}
fputs(filebuf, fp);
}
End:
if (fp != NULL) {
fclose(fp);
}
return rv;
}
}

main.c

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
#define _CRT_SECURE_NO_WARNINGS
#include"cfg_op.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define CFGNAME "d:/mycfg.ini"
//读配置项
int GetCfg() {
int ret = 0;
char name[1024] = { 0 };
char value[1024] = { 0 };
int vlen = 0;
printf("\n请输入Key:");
scanf("%s", name);
ret = GetCfgItem(CFGNAME, name, value, &vlen);
if (ret != 0) {
printf("func GetCfgItem err:%d \n", ret);
return ret;
}
printf("value:%s \n");
return ret;
}
//写配置项
int TWriteCfg() {
int ret = 0;
char name[1024] = { 0 };
char value[1024] = { 0 };
int vlen = 0;
printf("\n请输入Key:");
scanf("%s", name);
printf("\n请输入Value:");
scanf("%s", value);
WriteCfgItem(CFGNAME, name, value, &vlen);
if (ret != 0) {
printf("func WriteCfgItem err:%d \n", ret);
return ret;
}
printf("您的输入是:%s = %s \n", name, value);
return ret;
}

void show_menu() {
printf("==========================\n");
printf("1.测试写配置文件\n");
printf("2.测试读配置文件\n");
printf("0.退出\n");
printf("==========================\n");
}
int main() {
int choice;
show_menu();
while (~scanf("%d", &choice)) {
//显示一个菜单
switch (choice) {
case 1:
TWriteCfg();
break;
case 2:
GetCfg();
break;
default:
exit(0);
}
system("pause");
system("cls");
show_menu();
}
return 0;
}

文章结束了,但我们的故事还在继续
坚持原创技术分享,您的支持将鼓励我继续创作!