题目:
cfg.h
#pragma once
#ifdef __cplusplus
extern "C"
{
//函数声明
int ReadCfgFile(char *file, char *key, char *value, int *len);
int WriteCfgFile(char *file, char *key, char *value, int len);
}
#endif // __cplusplus
cfg.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 1024*8
int trimSpace(char *inbuf, char *outbuf)
{
if (inbuf == NULL || outbuf == NULL)
{
return -1;
}
char *p = inbuf;
int begin = 0;
int end = strlen(p) - 1;
int n = 0;
if (end < 0)
{
return -2;
}
//从左往右移动,如果当前字符为空,而且没有结束
while (isspace(p[begin]) && p[begin] != 0)
{
begin++;
}
while (isspace(p[end]) && end > 0 )
{
end--;
}
if (end == 0)
{
return -2;
}
n = end - begin + 1;//非空元素个数
strncpy(outbuf, p + begin, n);
outbuf[n] = 0;
return 0;
}
int ReadCfgFile(char *file, char *key, char *value, int *len)
{
if (file == NULL || key == NULL || value == NULL || len == NULL)
{
return -1;
}
FILE *fp = NULL;
int ret = 0;
char lineBuf[1024] = { 0 };
char *p = NULL;
int flag = 0; //0表示没有找到
//打开文件
fp = fopen(file, "r+");
if (fp == NULL)
{
ret = -2;
goto End;
}
while (1) {
//读取1行
if (fgets(lineBuf, sizeof(lineBuf), fp) == NULL)
{
break;
}
//lineBuf == "k1 = abc"
//找key
p = strstr(lineBuf, key);
if (p == NULL) //没有
{
continue; //跳出本次循环
}
//重新设置起点
p = p + strlen(key);
//找 "="
p = strstr(p, "=");
if (p == NULL) //没有
{
continue; //跳出本次循环
}
//重新设置起点
p = p + strlen("=");
//获取非空字符
ret = trimSpace(p, value);
if (ret == 0)//成功
{
*len = strlen(value);
flag = 1;
goto End;
}
else
{
goto End;
}
}
End:
if (fp != NULL)
{
fclose(fp);
}
if (flag == 0)
{
ret = -3;
printf("没有%s对应的value\n", key);
}
return ret;
}
int WriteCfgFile(char *file, char *key, char *value, int len)
{
if (file == NULL || key == NULL || value == NULL || len == NULL)
{
return -1;
}
FILE *fp = NULL;
int ret = 0;
char lineBuf[1024] = { 0 };
char buf[SIZE] = { 0 };
char *p = NULL;
int flag = 0; //0表示没有找到
fp = fopen(file, "r+");
if (fp == NULL) //打开失败,有可能是配置文件没有创建
{
//创建配置文件
fp = fopen(file, "w+");
if (fp == NULL)
{
ret = -2;
goto End;
}
}
//获取文件大小
//光标移动到结尾
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
if (size >= SIZE)
{
ret = -3;
printf("文件大小超过8k,不支持");
goto End;
}
//光标回到文件开头
rewind(fp);
while (1)
{
if (fgets(lineBuf, sizeof(lineBuf), fp) == NULL)
{
break;
}
//每一行是否包含key
if (strstr(lineBuf, key) != NULL)
{
flag = 1; //代表文件中已经有key值
sprintf(lineBuf, "%s = %s\n", key, value);
strcat(buf, lineBuf);
}
else
{
strcat(buf, lineBuf);
}
}
//程序到这,2种情况
if (0 == flag) //没有key
{
//文件光标已经移动到文件结尾
fprintf(fp, "%s = %s\n", key, value);
}
else //有key
{
//关闭文件
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
fp = fopen(file, "w+");
if (fp == NULL)
{
ret = -4;
goto End;
}
//重新写内容
fputs(buf, fp);
}
End:
if (fp != NULL)
{
fclose(fp);
}
return ret;
}
main.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cfg.h"
#define CFGNAME "mycfg.ini"
void menu()
{
printf("==========================\n");
printf("1、读配置文件\n");
printf("2、写配置文件\n");
printf("3、清屏\n");
printf("4、退出\n");
printf("==========================\n");
}
void MyRead()
{
char key[256] = { 0 };
char value[1024] = { 0 };
int len = 0;
int ret = 0;
printf("请输入key值:");
scanf("%s", key);
ret = ReadCfgFile(CFGNAME, key, value, &len);
if (ret != 0)
{
printf("ReadCfgFile err:%d\n", ret);
return;
}
printf("\n%s对应的value:%s, len = %d\n", key, value, len);
}
void MyWrite()
{
char key[256] = { 0 };
char value[1024] = { 0 };
int len = 0;
int ret = 0;
printf("请输入key值:");
scanf("%s", key);
printf("请输入value值:");
scanf("%s", value);
ret = WriteCfgFile(CFGNAME, key, value, strlen(value));
if (ret != 0)
{
printf("WriteCfgFile err:%d\n", ret);
return;
}
printf("\n写入成功,输入的是:%s对应的value:%s, len = %d\n", key, value, strlen(value));
}
int main(void)
{
int cmd;
while (1)
{
menu();
printf("cmd:");
scanf("%d", &cmd);
switch (cmd)
{
case 1:
MyRead();
break;
case 2:
MyWrite();
break;
case 3:
system("cls");
break;
default:
exit(0);
break;
}
}
printf("\n");
system("pause");
return 0;
}