首先在config.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
在config.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)
{
FILE* fp = NULL;
int ret = 0;
char lineBuf[1024] = { 0 };
char* p = NULL;
int flag = 0;//0代表没找到,1代表找到
//打开文件
fp = fopen(file, "r+");
if (fp == NULL)
{
ret = -1;
goto End;
}
while (1)
{
//读取1行
if (fgets(lineBuf, sizeof(lineBuf), fp) == NULL)
{
break;
}
//lineBuf = "player = 1"
//找key
p = strstr(lineBuf, key);
if (p == NULL)//没有
{
continue;//跳出本次循环
}
//重新设置起点
//"player = 1" -> "1"
p = p + strlen(key);
//找“=”
p = strstr(p, "=");
if (p == NULL)
{
continue;
}
//重新设置起点
//" = 1" -> "1"
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 (0 == flag)
{
ret = -3;
printf("没有%s对应的value\n", key);
}
return ret;
}
int WriteCfgFile(char* file, char* key, char* value, int len)
{
FILE* fp = NULL;
int ret = 0;
char lineBuf[1024] = { 0 };
char buf[SIZE] = { 0 };
char* p = NULL;
int flag = 0;//0代表没找到,1代表找到
//读写,“+”必须
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)
{//包含key
flag = 1;
sprintf(lineBuf, "%s = %s\n", key, value);
strcat(buf, lineBuf);
}
else
{
strcat(buf, lineBuf);
}
}
//程序到这,两种情况
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 <ctype.h>
#include "config.h"
#define CFGNAME "./config.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()
{
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;
case 4:
exit(0);
}
}
printf("\n");
system("pause");
return 0;
}