Linux c 配置文件读取与修改

2019-07-12 21:54发布

修改自某新手的有BUG的程序,新建个配置文件configure.cfg在当前目录,然后,然后复制代码去感受下吧 #include #include #include /* *从配置文件中读取相应的值 *输入参数:1,配置文件路径 2,匹配标记 ,返回读取的变量 *并且排除了空行,“=”前后无内容,无“=”的情况 */ char * ReadConfig(char *conf_path,char *conf_name) { char *config_buff; char config_linebuf[256]; char line_name[40]; char exchange_buf[256]; char *config_sign = "="; char *leave_line; FILE *f; f = fopen(conf_path,"rw"); if(f == NULL) { printf("Open configure file fail! "); return 0; } fseek(f,0,SEEK_SET); while(fgets(config_linebuf,256,f) != NULL) { if(strlen(config_linebuf) < 3) //判断是否是空行 { continue; } if (config_linebuf[strlen(config_linebuf)-1] == 10) //去除最后一位是 的情况 { memset(exchange_buf,0,sizeof(exchange_buf)); strncpy(exchange_buf,config_linebuf,strlen(config_linebuf)-1); memset(config_linebuf,0,sizeof(config_linebuf)); strcpy(config_linebuf,exchange_buf); } memset(line_name,0,sizeof(line_name)); leave_line = strstr(config_linebuf,config_sign); if(leave_line == NULL) //去除无"="的情况 { continue; } int leave_num = leave_line - config_linebuf; strncpy(line_name,config_linebuf,leave_num); if(strcmp(line_name,conf_name) ==0) { strncpy(config_buff,config_linebuf+(leave_num+1),strlen(config_linebuf)-1); break; } if(fgetc(f)==EOF) { break; } fseek(f,-1,SEEK_CUR); memset(config_linebuf,0,sizeof(config_linebuf)); } fclose(f); return config_buff; } /* *添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加) * *输入参数:1,配置文件路径 2,匹配标记 3,替换或添加的内容 * */ AddOrAltConfig(char *conf_path,char *conf_name,char *config_buff) { char config_linebuf[256]; char line_name[40]; char *config_sign = "="; char *leave_line; int alter_sign = 0; FILE *f; f = fopen(conf_path,"r+"); if(f == NULL) { printf("Open configure fail! "); return 0; } fseek(f,0,SEEK_END); long congig_lenth = ftell(f); int configbuf_lenth = strlen(config_buff); configbuf_lenth = configbuf_lenth + 5; char sum_buf[congig_lenth+configbuf_lenth]; memset(sum_buf,0,sizeof(sum_buf)); fseek(f,0,SEEK_SET); while(fgets(config_linebuf,256,f) != NULL) { if(strlen(config_linebuf) < 3) //判断是否是空行 { strcat(sum_buf,config_linebuf); continue; } leave_line = NULL; leave_line = strstr(config_linebuf,config_sign); if(leave_line == NULL) //去除无"="的情况 { strcat(sum_buf,config_linebuf); continue; } int leave_num = leave_line - config_linebuf; memset(line_name,0,sizeof(line_name)); strncpy(line_name,config_linebuf,leave_num); if(strcmp(line_name,conf_name) ==0) { strcat(sum_buf,conf_name); strcat(sum_buf,"="); strcat(sum_buf,config_buff); strcat(sum_buf," "); alter_sign = 1; } else { strcat(sum_buf,config_linebuf); } if(fgetc(f)==EOF) { break; } fseek(f,-1,SEEK_CUR); memset(config_linebuf,0,sizeof(config_linebuf)); } if(alter_sign == 0) { strcat(sum_buf,config_buff); strcat(sum_buf," "); } printf("---configure----> %s<---------- ",sum_buf); remove(conf_path); fclose(f); FILE *fp; fp = fopen(conf_path,"w+"); if(fp == NULL) { printf("Open configure fail! "); return 2; } fseek(fp,0,SEEK_SET); fputs(sum_buf,fp); fclose(fp); } int main(void) { char* data; char* file = "./configure.cfg"; data = ReadConfig(file,"id"); printf("id = %s ",data); AddOrAltConfig(file,"id","678"); }