c语言实现配置文件的读写
文件名 | 作用 |
---|---|
global.h | 定义一些宏 |
func.h | 公用函数头文件 |
func.c | 公用函数的实现 |
config.h | 读写配置文件的函数声明 |
config.c | 读写配置文件的函数实现 |
config_main.c | 主函数入口 |
config.conf | 配置文件 |
直接上代码,一些解释以注释的形式写在了相应的文件里面。
// 文件名:global.h #ifndef __global_h__ #define __global_h__ #define bool int #define false 0 #define true 1 #endif // !__global_h__
// 文件名:func.h #ifndef __func_h__ #define __func_h__ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include "global.h" void rtrim(char* sstring); void ltrim(char* sstring); //比较str1和str2两个字符串内容是否一样,忽略大小写的比较 bool strcasecmp(const char* str1, const char* str2); //把str全都变成大写 void stringcase(char* str); #endif // !__func_h__
// 文件名:func.c #include "func.h" //截取字符串尾部空格 void rtrim(char *sstring) { size_t len = 0; if (sstring == null) return; len = strlen(sstring); while (len > 0 && sstring[len - 1] == ' ')//位置换一下 sstring[--len] = 0; } //截取字符串首部空格 void ltrim(char *sstring) { size_t len = 0; if (sstring == null) return; char *p_tmp = sstring; //此时p_tmp指向了sstring if ((*p_tmp) != ' ') return; //不是以空格开头 //找到第一个不为空格的位置 while ((*p_tmp) != '\0') { if ((*p_tmp) == ' ') p_tmp++; else break; } if ((*p_tmp) == '\0') //全是空格 { *sstring = '\0'; return; } char *p_tmp2 = sstring; //此时 p_tmp2 指向了 sstring while ((*p_tmp) != '\0') { (*p_tmp2) = (*p_tmp); p_tmp++; p_tmp2++; } (*p_tmp2) = '\0'; //空格处理完毕,记得在末尾添加一个'\0' 表示字符串结尾 return; } //比较str1和str2两个字符串内容是否一样,忽略大小写的比较 bool strcasecmp(const char* str1, const char* str2) { if (str1 == null || str2 == null) return false; size_t strlen1 = strlen(str1); size_t strlen2 = strlen(str2); if (strlen1 != strlen2) return false; // char *str = new char[strlen2]; char *str = (char*)malloc(strlen2); strcpy(str, str2); //把str2的内容拷贝到str中 stringcase(str); //把str中的所有内容转成大写字母 while ((*str) != '\0') { if ((*str1) > 90) { if (((*str1) - 32) == (*str)) { ++str1; ++str; } else { return false; } } else { if ((*str1) == (*str)) { ++str1; ++str; } else { return false; } } } free(str); return true; } //把str中所有的小写字母变成大写 void stringcase(char* str) { if (str == null) return ; if (strlen(str) <= 0) return ; int strlen = strlen(str); char *ptmp = str; while ((*ptmp) != '\0') { if ((97 <= (*ptmp)) && ((*ptmp) <= 122)) { (*ptmp) -= 32; } ++ptmp; } return; }
// 文件名:config.h #ifndef __config_h__ #define __config_h__ #include "global.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include "func.h" #include <fcntl.h> #include <unistd.h> // item数组的大小,也是配置文件中有效的item项目的最大值 #define configstructsize 80 #define confignamesize 32 #define configcontentsize 512 struct configstruct{ char itemname[confignamesize]; char itemcontent[configcontentsize]; }; // 加载配置文件 int loadconfigfile(const char* pconfname); // 通过item名字获取item的内容 const char* getconfigfromstring(const char* p_itemname); // 通过item名字获取item内容,整型,获取不到返回默认值 int getconfigintdefault(const char* p_itemname, const int def); // // 释放内存 // void freeconfigitem(); // 写配置文件 // 通过item名字修改item内容 int modifyconfigitemcontent(const char* p_itemname, const char* p_itemcontent); // 通过item名字和内容 添加新的item项目 int addconfigitem(const char*p_itemname, const char* p_itemcontent); // 把加载进来的、或者有修改过了、增加过的 item数组写到指定的文件 int writenewconfigfile(const char*pconfname); #endif // !__config_h__
// 文件名:config.c #include <config.h> static int arr_curr_ind = 0; static struct configstruct arrayconfig[configstructsize]; int loadconfigfile(const char* pconfname) { if(arr_curr_ind > 0){ return 0; } file *fp; fp = fopen(pconfname, "r"); if(fp == null) return false; // 每一行配置文件读取出来放到这里 unsigned int linebuf_size = configcontentsize + 1 + confignamesize + 1 + 1; char linebuf[linebuf_size]; // 128+1 结尾为结束符 memset(linebuf, 0, sizeof(linebuf)); while(!feof(fp)) { if(fgets(linebuf, linebuf_size, fp) == null) continue; if(linebuf[0] == 0) continue; if(*linebuf == ';' || *linebuf == ' ' || *linebuf == '#' || *linebuf == '\t' || *linebuf == '\n') continue; // 去除字符串中的 \r \n 以及空格' ' lblprocstring: if(strlen(linebuf) > 0) { if(linebuf[strlen(linebuf) - 1] == 10 || linebuf[strlen(linebuf) - 1] == 13 || linebuf[strlen(linebuf) - 1] == 32 ) { linebuf[strlen(linebuf) - 1] = 0; goto lblprocstring; } } if(linebuf[0] == 0) continue; if(*linebuf == '[') // [ 开头的注释,也保存,方便以后写文件时写回去 { if(arr_curr_ind < configstructsize) { strcpy(arrayconfig[arr_curr_ind].itemname, linebuf); strcpy(arrayconfig[arr_curr_ind].itemcontent, " "); // arrayconfig[arr_curr_ind] = p_configitem; arr_curr_ind += 1; } else { // error } continue; } // 到这里,都是合法的配置项 char *ptmp = strchr(linebuf, '='); if(ptmp != null) { if(arr_curr_ind < configstructsize) { strncpy(arrayconfig[arr_curr_ind].itemname, linebuf, (int)(ptmp-linebuf)); strcpy(arrayconfig[arr_curr_ind].itemcontent, ptmp+1); rtrim(arrayconfig[arr_curr_ind].itemname); ltrim(arrayconfig[arr_curr_ind].itemname); rtrim(arrayconfig[arr_curr_ind].itemcontent); ltrim(arrayconfig[arr_curr_ind].itemcontent); arr_curr_ind += 1; } else { // error } } // end if } // end while fclose(fp); return 1; } const char* getconfigfromstring(const char* p_itemname) { int i = 0; for(i = 0; i < arr_curr_ind; i++) { if(strcmp(p_itemname, arrayconfig[i].itemname) == 0) { return arrayconfig[i].itemcontent; } } return null; } int getconfigintdefault(const char* p_itemname, const int def) { int i; for(i = 0; i < arr_curr_ind; i++) { if(strcmp(p_itemname, arrayconfig[i].itemname) == 0) { return atoi(arrayconfig[i].itemcontent); } } return def; } int modifyconfigitemcontent(const char* p_itemname, const char* p_itemcontent) { if (!p_itemname || !p_itemcontent) return 0; int ret_res = 0; int i = 0; for(i = 0; i < arr_curr_ind; i++) { if(strcmp(p_itemname, arrayconfig[i].itemname) == 0) { strcpy(arrayconfig[i].itemcontent, p_itemcontent); ret_res = 1; } } return ret_res; } int addconfigitem(const char*p_itemname, const char* p_itemcontent) { if (!p_itemname || !p_itemcontent) return 0; int i; int ret_res = 0; for(i=0; i<arr_curr_ind; ++i) { ret_res = modifyconfigitemcontent(p_itemname, p_itemcontent); if(ret_res == 1) return ret_res; } ret_res = 0; if(arr_curr_ind < configstructsize) { strcpy(arrayconfig[arr_curr_ind].itemname, p_itemname); strcpy(arrayconfig[arr_curr_ind].itemcontent, p_itemcontent); arr_curr_ind += 1; ret_res = 1; } else { ret_res = 0; } return ret_res; } int writenewconfigfile(const char*pconfname) { if(!pconfname) return 0; int new_fd; if(-1 == (new_fd = open(pconfname,o_rdwr|o_creat|o_trunc,0664))) { return 0; } int i = 0; char line_buf[32+128+1] = "\0"; char equal_char = '='; for(i=0; i < arr_curr_ind; i++) { if(*(arrayconfig[i].itemname) == '[') equal_char = ' '; else equal_char = '='; sprintf(line_buf, "%s%c%s\n", arrayconfig[i].itemname, equal_char, arrayconfig[i].itemcontent); write(new_fd, line_buf, strlen(line_buf)); memset(line_buf, 0, sizeof(line_buf)); } close(new_fd); return 1; }
// 文件名:config_main.c #include "global.h" #include "config.h" #include "func.h" #include <time.h> int main() { const char* config_file = "./config.conf"; loadconfigfile(config_file); const char *ip = getconfigfromstring("server_url"); const int port = getconfigintdefault("server_port", 80); if (ip) printf("url:%s\t", ip); printf("port:%d\n", port); printf("device_id:%s\n", getconfigfromstring("device_id")); printf("device_name:%s\n", getconfigfromstring("device_name")); printf("serv_pub_info_url=%s\n", getconfigfromstring("serv_pub_info_url")); printf("serv_pub_curr_log=%s\n", getconfigfromstring("serv_pub_curr_log")); printf("serv_pub_hist_log=%s\n", getconfigfromstring("serv_pub_hist_log")); printf("certificate=%s\n", getconfigfromstring("certificate")); return 0; }
文件名:config.conf
[服务器地址] server_url=192.168.137.1 server_port=80 [信息推送地址] serv_regi_dev_url=/regist_dev serv_pub_info_url=/pub_dev_info serv_pub_curr_log=/pub_current_log serv_pub_hist_log=/pub_history_log [设备信息] device_id=20210830_1400_00001 device_name=dev_001 [设备证书] certificate=config_brkzh.github.io_config [设备是否注册] registry_flag=0 ct=65534
把以上的代码都存到 对应文件 里面,并且放到 同一个目录 下。
编译指令:gcc -o my_conf config_main.c config.c func.c -i ./
运行指令:./my_config
运行结果:
注意:此份代码仅供学习使用,并没有什么商业价值。
因为这段代码
// item数组的大小,也是配置文件中有效的item项目的最大值 #define configstructsize 80 #define confignamesize 32 #define configcontentsize 512 struct configstruct{ char itemname[confignamesize]; char itemcontent[configcontentsize]; }; static struct configstruct arrayconfig[configstructsize];
这里设定了配置文件的最大项目数量等,一开始就创建这么多数组,用于保存配置文件,这些开销都是在栈上。没有使用malloc动态分配内存,所以代码仅供学习。
到此这篇关于详解c语言如何实现配置文件的读写的文章就介绍到这了,更多相关c语言读写配置文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论