c语⾔读写配置⽂件
配置⽂件的格式如下:
key1 = value1
key2 = value2
中间及前后可以有多个空格
思路分析:
读写配置⽂件可以分成底层API接⼝和调⽤API的界⾯⼆个模块,⼆个模块间耦合度要尽量低,底层封装的API要尽量好⽤。不要让⼈家写⽂件的时候还需要⾃⼰去判断配置项是否已经存在,这些功能底层API要做完善。
⼀接⼝设计
既然是读写,提供给外部⼀个读取的接⼝和⼀个写⼊的接⼝就可以了。要让⼆个接⼝⽅便好⽤
int writeCFG(const char *filename/*in*/, const char *key/*in*/, const char *value/*in*/);
//写⼊配置⽂件
怎样培养孩子注意力void readCFG(const char *filename/*in*/, const char *key/*in*/, const char **value/*out*/);
//读取配置⽂件
⼆框架搭建
ReadAndWrite.h
函数原型
void trim(char *strIn, char *strOut);//去除字符串前⾯和后⾯的空格
void getValue(char * keyAndValue, char * key, char * value);
//根据key得到value
int writeCFG(const char *filename/*in*/, const char *key/*in*/, const char *value/*in*/);
//写⼊配置⽂件
void readCFG(const char *filename/*in*/, const char *key/*in*/, const char **value/*out*/);
//读取配置⽂件
三代码实现
ReadAndWrite.c
函数实现
void trim(char *strIn, char *strOut){
char *start, *end, *temp;//定义去除空格后字符串的头尾指针和遍历指针
temp = strIn;
while (*temp == ''){
++temp;
}
start = temp; //求得头指针
temp = strIn + strlen(strIn) - 1; //得到原字符串最后⼀个字符的指针(不是'\0')
while (*temp == ''){
--temp;
}
end = temp; //求得尾指针
for(strIn = start; strIn <= end; ){
*strOut++ = *strIn++;
}
*strOut = '\0';
}
void getValue(char * keyAndValue, char * key, char * value){
char *p = keyAndValue;
p = strstr(keyAndValue, key);//到key的位置,指针
if(p == NULL){
printf("没有key %s\n", key);
return ;
}
p += strlen(key);//指针后移到key后⾯
trim(p, value);
p = strstr(value, "=");//等号的位置
if(p == NULL){
printf("没有到=\n");
return;
}
p+= strlen("=");//指针后移到等号后⾯
trim(p, value);//删除字符串前后的空格
}
int writeCFG(const char *filename/*in*/, const char *key/*in*/, const char *value/*in*/){ FILE *pf = NULL;
char ftemp[flen] = {0}, fline[1024] = {0}, *fp; //⽂件缓存数组
long fsize = 0;
int reg = 0;
int exit = 0;
int i = 0;
int flen = 8*1024;
pf = fopen(filename, "r+");
if(pf == NULL){
pf = fopen(filename, "w+");
}
//获得⽂件⼤⼩
fseek(pf, 0, SEEK_END); // 将⽂件指针pf指向末尾
fsize = ftell(pf);//获取⽂件开头到pf的长度
if(fsize > flen){
printf("⽂件不能超过8k\n");
reg = -1;
goto end;
}
fseek(pf, 0, SEEK_SET); //将⽂件指针指向开头
//⼀⾏⼀⾏的读,如果存在key则修改value存到缓存数组中
while(!feof(pf)){//未到⽂件结尾
fgets(fline, 1024, pf);
if(strstr(fline, key) != NULL && exit == 1)
strcpy(fline, "");
if(strstr(fline, key) != NULL && exit == 0){ //判断key是否存在
exit = 1;
sprintf(fline,"%s = %s\n", key, value);
}
printf("fline = %s\n", fline);
strcat(ftemp, fline);
}
if(exit != 1){//如果不存在则把key value写⼊到最后⼀⾏
sprintf(fline,"%s = %s\n", key, value);
strcat(ftemp, fline);
}
if(pf != NULL){
fclose(pf);九阳料理机使用方法
pf = fopen(filename, "w+");
fp = (char *)malloc(sizeof(char) * strlen(ftemp) + 1);
strcpy(fp, ftemp);
fp[strlen(fp) - 1] = EOF;
fputs(fp, pf);
if(fp != NULL){
free(fp);
fp = NULL;
}
fclose(pf);
}
running manend :
if(pf != NULL)
fclose(pf);
//重新创建⼀个以filename命名的⽂件
return reg;
}
void readCFG(const char *filename/*in*/, const char *key/*in*/, const char **value/*out*/){ FILE *pf = NULL;
char line[1024] = {0}, vtemp[1024] = {0};
pf = fopen(filename, "r"); //以只读⽅式打开
while(!feof(pf)){
fgets(line, 1024, pf);
getValue(line, key, vtemp);
if(strlen(vtemp) != 0)
break;
}
if(strlen(vtemp) != 0){
*value = (char *)malloc(sizeof(char) * strlen(vtemp) + 1);
strcpy(*value, vtemp);
}
else
*value = NULL;
if(pf != NULL)
fclose(pf);
}
测试界⾯代码:
#define filename "c:/cfg.ini"
void menu(){
printf("===========================\n");
printf("1 写⼊配置⽂件\n");
printf("2 读取配置⽂件\n");
printf("0 退出程序");
30岁女人保养printf("===========================\n");
}
int tWrite(){
char key[1024] = {0}, value[1024] = {0};
printf("请输⼊key:");
scanf("%s", key);
printf("请输⼊value:");
scanf("%s", value);
printf("\n您输⼊的是:%s = %s\n", key, value);
return writeCFG(filename/*in*/,key/*in*/,value/*in*/);
}
void tRead(){
char key[1024] = {0}, *value;
printf("请输⼊key:");
scanf("%s", key);
readCFG(filename/*in*/,key/*in*/, &value/*out*/);
if(value == NULL){
printf("没有key\n");
return ;
}
printf("\nvalue = %s\n", value);
if(value != NULL){
free(value);
value = NULL;
}
九月九日忆山东兄弟古诗}
int main(){
int choose;
while(1){
choose = 0;
menu();
printf("请输⼊选择:");
scanf("%d", &choose);
生活文章switch(choose){
case1:
if(tWrite() == -1)
return -1;
break;
case2:
tRead();
break;
case0:
return0;
default:
return0;
}
}
system("pause"); return0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论