配置文件的读写

这篇博客介绍了如何操作配置文件,包括添加和修改教师信息。代码中定义了两个函数`AddTeacher`和`ModifyTeacher`,分别用于保存和显示教师的ID、姓名、年龄和学生信息。这两个函数通过关键字动态构建并读写配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// myop.h

#ifndef _INC_MY_OP_H
#define _INC_MY_OP_H
#ifdef  __cplusplus
extern "C" {
#endif


typedef struct _Teacher
{
int infoId;
char name[64];
int age;
char studentInfo[1024]; //s1,s2,s3,
}Teacher;


//添加老师
int AddTeacher(char *pfileName, Teacher *pTe);

//显示老师
int ModifyTeacher(char *pfileName, Teacher *pTe);


#ifdef  __cplusplus
}

#endif

#endif 


myop.c

#define _CRT_SECURE_NO_WARNINGS
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
//include "cfg_op.h"


#include "myop.h"
//添加老师
int AddTeacher(char *pfileName, Teacher *pTe)
{
int ret = 0;
//准备写配置文件的关键字 根据ID动态构建
char Teacher_InfoID[128];
char Teacher_Name[128];
char Teacher_Age[128];
char Teacher_StudentInfo[1024];
char infoId[128];
char infoAge[128];


sprintf(Teacher_InfoID, "%s%d", "Teacher_InfoID_", pTe->infoId);
sprintf(Teacher_Name, "%s%d", "Teacher_Name_", pTe->infoId);
sprintf(Teacher_Age, "%s%d", "Teacher_Age_", pTe->infoId);
sprintf(Teacher_StudentInfo, "%s%d", "Teacher_StudentInfo_", pTe->infoId);


sprintf(infoId, "%d", pTe->infoId);
sprintf(infoAge, "%d", pTe->age);


ret = SetCfgItem(pfileName, Teacher_InfoID, infoId, strlen(infoId));
if (ret != 0)
{
printf("func  SetCfgItem() err: %d \n", ret);
return ret;
}


ret = SetCfgItem(pfileName, Teacher_Name, pTe->name, strlen(pTe->name));
if (ret != 0)
{
printf("func  SetCfgItem() err: %d \n", ret);
return ret;
}
ret = SetCfgItem(pfileName, Teacher_Age, infoAge, strlen(infoAge));
if (ret != 0)
{
printf("func  SetCfgItem() err: %d \n", ret);
return ret;
}
ret = SetCfgItem(pfileName, Teacher_StudentInfo, pTe->studentInfo, strlen(Teacher_StudentInfo));
if (ret != 0)
{
printf("func  SetCfgItem() err: %d \n", ret);
return ret;
}
return ret;
}


//显示老师
int ModifyTeacher(char *pfileName, Teacher *pTe)
{


int ret = 0;
//准备写配置文件的关键字 根据ID动态构建
char Teacher_InfoID[128];
char Teacher_Name[128];
char Teacher_Age[128];
char Teacher_StudentInfo[1024];
char infoId[128] = { 0 };
char infoAge[128] = { 0 };
int tmpLen = 0;


sprintf(Teacher_InfoID, "%s%d", "Teacher_InfoID_", pTe->infoId);
sprintf(Teacher_Name, "%s%d", "Teacher_Name_", pTe->infoId);
sprintf(Teacher_Age, "%s%d", "Teacher_Age_", pTe->infoId);
sprintf(Teacher_StudentInfo, "%s%d", "Teacher_StudentInfo_", pTe->infoId);


sprintf(infoId, "%d", pTe->infoId);
sprintf(infoAge, "%d", pTe->age);




ret = GetCfgItem(pfileName, Teacher_InfoID, infoId, &tmpLen);
if (ret != 0)
{
printf("func GetCfgItem() err: %d \n", ret);
return ret;
}


ret = GetCfgItem(pfileName, Teacher_Name, pTe->name, &tmpLen);
if (ret != 0)
{
printf("func GetCfgItem() err: %d \n", ret);
return ret;
}
ret = GetCfgItem(pfileName, Teacher_Age, infoAge, &tmpLen);
if (ret != 0)
{
printf("func GetCfgItem() err: %d \n", ret);
return ret;
}
pTe->age = atoi(infoAge);


ret = GetCfgItem(pfileName, Teacher_StudentInfo, pTe->studentInfo, &tmpLen);
if (ret != 0)
{
printf("func GetCfgItem() err: %d \n", ret);
return ret;
}


return 0;
}
void mainxxx()
{
system("pause");
}


test_interface.c


#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
//#include "cfg_op.h"
#include "myop.h"




void main_menu()
{
printf("\n1 Test AddTeacher(测试保存老师信息) ");
printf("\n2 Test ModifyTeacher(测试显示老师信息) ");
printf("\n0 exit(0)        (程序退出)    ");
printf("\nplease enter your choice:[1/2/0]: ");
return;
}


int  Test_AddTeacher()
{
int rv = 0;
char *pfileName = "C:\teacherinfo.ini";
Teacher t1;
memset(&t1, 0, sizeof(Teacher));
t1.infoId = 1;
t1.age = 11;
strcpy(t1.name, "name");
strcpy(t1.studentInfo, "s1,s2,s3,");


//添加老师
rv = AddTeacher(pfileName, &t1);
if (rv != 0)
{
printf("fucn AddTeacher() err:%d \n", rv);
return rv;
}


printf("读写配置项绿灯测试通过\n");


End:
return rv;


}


int  Test_ModifyTeache()
{


int rv = 0;
char *pfileName = "c:/teacherinfo.ini";
Teacher t1;
memset(&t1, 0, sizeof(Teacher));
t1.infoId = 1;
rv = ModifyTeacher(pfileName, &t1);
if (rv != 0)
{
printf("fucn ModifyTeacher() err:%d \n", rv);
return rv;
}
printf("\n读写配置项绿灯测试通过\n");


End:
return rv;


}


int  main()
{
int rv = 0;
int choice = 0;


for (;;)
{
main_menu();
scanf("%d", &choice);
switch (choice)
{
case 1:
Test_AddTeacher();   break;
case 2:
Test_ModifyTeache();   break;
case 0:
exit(0);
default:
exit(0);
}
}


End:
return rv;
getchar();
}

cfg_op.h

// cfg_op.h


#ifndef _INC_CFG_OP_H
#define _INC_CFG_OP_H


#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 *pItemName /*in*/, char *pItemValue/*in*/, int itemValueLen /*in*/);




//int CfgItem_Init(void *pHandle, int iType);
//int GetCfgItem(void *pHandle /*in*/, char *pKey /*in*/, char * pValue/*in out*/, int * pValueLen /*out*/);
//int WriteCfgItem(void *pFileName /*in*/, char *pItemName /*in*/, char *pItemValue/*in*/, int itemValueLen /*in*/);
//int CfgItem_Destory(void *pHandle);


#ifdef  __cplusplus
}
#endif


#endif 

cfg_op.c

#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"




int GetCfgItem(char *pFileName /*in*/, char *pKey /*in*/, char * pValue/*in out*/, int * pValueLen /*out*/);
int WriteCfgItem(char *pFileName /*in*/, char *pItemName /*in*/, char *pItemValue/*in*/, int itemValueLen /*in*/);


//实现流程
//打开文件
//按照行的方式 循环读文件
//解析每一行,若匹配key关键字,在进行value值的提取
//提取value值需要 1去除前后空格 1级指针典型应用


#define  LineMaxLen 2048
#define  KeyMaxLen 64


int GetCfgItem(char *pFileName /*in*/, char *pKey /*in*/, char * pValue/*in out*/, int * pValueLen /*out*/)
{
int rv = 0;
FILE *fp = NULL;
char lineBuf[LineMaxLen];
char *pTmp = NULL, *pBegin = NULL, *pEnd = NULL;


if (pFileName==NULL || pKey==NULL || pValue==NULL || pValueLen==NULL) 
{
rv = -1;
printf("GetCfgItem() err. param err \n");
goto End;
}


fp = fopen(pFileName, "r");
if (fp == NULL)
{
rv = -2;
printf("fopen() err. \n");
goto End;
}
while (!feof(fp))
{
//读每一行
memset(lineBuf, 0, sizeof(lineBuf));
pTmp = fgets(lineBuf, LineMaxLen, fp);
if (pTmp == NULL) 
{
break;
}


//不含=, 非配置项
pTmp = strchr(lineBuf, '=');
if (pTmp == NULL)
{
continue;
}
//key是否在本行
pTmp = strstr(lineBuf, pKey);
if (pTmp == NULL)
{
continue;
}

//调整到=右边,取value准备
pTmp = strchr(lineBuf, '=');
if (pTmp == NULL)
{
continue;
}
pTmp = pTmp + 1;


//获取value 起点
while (1) 
{
if (*pTmp == ' ')
{
pTmp ++ ;

else
{
pBegin = pTmp;
if (*pBegin == '\n')
{
//没有配置value
printf("配置项:%s 没有配置value \n", pKey);
goto End;
}
break;
}
}

//获取valude结束点
while (1) 
{
if ((*pTmp == ' ' || *pTmp == '\n'))
{
break;
}
else 
{
pTmp ++;
}
}
pEnd = pTmp;


//赋值
*pValueLen = pEnd-pBegin;
memcpy(pValue, pBegin, pEnd-pBegin);
break;
}


End:
if (fp != NULL)
{
fclose(fp); 
}

return rv;
}


//实现流程
//循环读每一行,检查key配置项是否存在 若存在修改对应value值
//若不存在,在文件末尾 添加 "key = value"
//难点:如何修改文件流中的值


int SetCfgItem(char *pFileName /*in*/, char *pKey /*in*/, char * pValue/*in*/, int ValueLen /*in*/)
{
int rv = 0, iTag = 0, length = 0;
FILE *fp = NULL;
char lineBuf[LineMaxLen];
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");
//goto End;
}


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, LineMaxLen, fp);
if (pTmp == NULL) 
{
break;
}

//key关键字是否在本行
pTmp = strstr(lineBuf, pKey);
if (pTmp == NULL)
{
strcat(filebuf, lineBuf);
continue;
}
else
{
sprintf(lineBuf, "%s = %s\n", pKey, pValue);
strcat(filebuf, lineBuf);
//若存在key
iTag = 1; 
}
}


//若不存在 追加
if (iTag == 0) 
{
fprintf(fp, "%s = %s\n", pKey, pValue);
}
else //若存在
{
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);
//fwrite(filebuf, sizeof(char), strlen(filebuf), fp);
}

End:
if (fp != NULL)
{
fclose(fp); 
}
return rv;
}



纯c读写ini配置文件 用c/c++读写ini配置文件有不少第三方的开源库,如iniparser、libini、rwini、UltraLightINIParser等,但都不理想,往往代码较大、功能较弱、 接口使用不方便。尤其在大小写处理、前后空格、各种注释、跨平台换行符支持、带引号字符串处理、无section操作、原格式保持等方面存在问题。 现将本人精心制作的ini读写程序源码奉献给大家,纯c编写,简洁好用。支持windows和linux。 主要特点: 1、支持;和#注释符号,支持行尾注释。 2、支持带引号'或"成对匹配的字符串,提取时自动去引号。引号中可带其它引号或;#注释符。 3、支持无section或空section(名称为空)。 4、支持10、16、8进制数,0x开头为16进制数,0开头为8进制。 5、支持section、key或=号前后带空格。 6、支持\n、\r、\r\n或\n\r换行格式。 7、不区分section、key大小写,但写入时以新串为准,并保持其大小写。 8、新增数据时,若section存在则在该节最后一个有效数据后添加,否则在文件尾部添加。 9、支持指定key所在整行删除,即删除该键值,包括注释。 10、可自动跳过格式错误行,修改时仍然保留。 11、修改时保留原注释:包括整行注释、行尾注释(包括前面空格)。 12、修改时保留原空行。以上三点主要是尽量保留原格式。 不足之处: 1、不支持单key多value(逗号分割),只能一次性提取后自行处理。 2、不支持同名重复section和key。(重复section可视为错误,重复key则可能造成分歧) 3、不能提取所有section或key名称。 使用只需两个文件inirw.h、inirw.c,另有测试程序和工程文件,支持windows和linux。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值