











C语言在Linux中开发读取配置文件app.conf
在工程根目录下创建app.conf文件
注:app.conf 和 MyCServer.c 必须在同一目录下

app.conf
# app.conf 全局配置文件 # 格式: Key=Value (支持首尾空格,注释用 # 或 ;) TimerEnabled = 1 LogLevel = 2 MaxConnections = 200 ServerIP = 192.168.1.100 ServerPort = 9000 LogDir = /var/log/mycserver
MyCServer.h
//集中定义所有配置项(按需增删) typedef struct { int timer_enabled; // 定时器开关:0/1 int log_level; // 日志级别:0~3 int max_connections; // 最大连接数 char server_ip[32]; // 监听IP int server_port; // 监听端口 char log_dir[128]; // 日志目录 // ... 后续新增配置项直接在此添加 ... } AppConfig; /*======== 函数声明 ========= */ int LoadAppConfig(const char* path, AppConfig* cfg);
MyCServer.c
定义全局配置实例
/*====================== 常量 ======================注:常量后面没有分号 */ #define CONFIG_FILE_PATH "app.conf" //配置文件路径(建议与可执行文件同目录) // 全局配置实例(初始化为0,加载时会覆盖) AppConfig g_cfg = {0};
主函数
int main(void){ //加载app.conf配置文件并赋值到g_cfg LoadAppConfig(CONFIG_FILE_PATH, &g_cfg); printf("g_cfg.timer_enabled 开关:%d\n", g_cfg.timer_enabled); return 0; }
读配置文件函数 注:app.conf 和 MyCServer.c 必须在同一目录下
/** * @brief 安全解析配置文件,填充 SysConfig 结构体 * @param path 配置文件路径 * @param cfg 输出参数:配置结构体指针 * @return 0 成功,-1 文件缺失(使用默认值) */ int LoadAppConfig(const char* path, AppConfig* cfg) { //1. 设置安全默认值(防止配置缺失导致未定义行为) cfg->timer_enabled = 0; cfg->log_level = 2; cfg->max_connections = 100; snprintf(cfg->server_ip, sizeof(cfg->server_ip), "0.0.0.0"); cfg->server_port = 8080; snprintf(cfg->log_dir, sizeof(cfg->log_dir), "./logs"); FILE* fp = fopen(path, "r"); if (!fp) { Log_Write("[配置][警告] 未找到 %s,已启用全部默认值", path); return -1; } char line[512]; while (fgets(line, sizeof(line), fp)) { // 跳过行首空白 char* p = line; while (*p && isspace((unsigned char)*p)) p++; // 跳过注释(# / ;) 和空行 if (*p == '\0' || *p == '#' || *p == ';') continue; // 查找等号分隔符 char* eq = strchr(p, '='); if (!eq) continue; *eq = '\0'; // 截断,此时 p 指向 key,eq+1 指向 value char* key = p; char* val = eq + 1; // 清理 key 尾部空格 char* k_end = key + strlen(key) - 1; while (k_end > key && isspace((unsigned char)*k_end)) *k_end-- = '\0'; // 清理 val 首尾空格 while (*val && isspace((unsigned char)*val)) val++; char* v_end = val + strlen(val) - 1; while (v_end >= val && isspace((unsigned char)*v_end)) *v_end-- = '\0'; //核心匹配区(按需扩展 if-else 链) if (strcmp(key, "TimerEnabled") == 0) { //C语言的布尔归一化逻辑 只要 atoi(val) 不是 0,就强制赋值为 1。 //cfg->timer_enabled = (atoi(val) != 0) ? 1 : 0; cfg->timer_enabled = atoi(val); } else if (strcmp(key, "LogLevel") == 0) { int lvl = atoi(val); cfg->log_level = (lvl >= 0 && lvl <= 3) ? lvl : 2; } else if (strcmp(key, "MaxConnections") == 0) { cfg->max_connections = atoi(val); if (cfg->max_connections <= 0) cfg->max_connections = 100; } else if (strcmp(key, "ServerIP") == 0) { strncpy(cfg->server_ip, val, sizeof(cfg->server_ip) - 1); cfg->server_ip[sizeof(cfg->server_ip) - 1] = '\0'; } else if (strcmp(key, "ServerPort") == 0) { int port = atoi(val); cfg->server_port = (port > 0 && port < 65536) ? port : 8080; } else if (strcmp(key, "LogDir") == 0) { strncpy(cfg->log_dir, val, sizeof(cfg->log_dir) - 1); cfg->log_dir[sizeof(cfg->log_dir) - 1] = '\0'; } else { Log_Write("[配置][警告] 忽略未知配置项: %s", key); } } fclose(fp); //打印加载摘要 Log_Write("[配置] 加载完成 -> TimerEnabled=%d, LogLevel=%d, IP=%s:%d, MaxConn=%d", cfg->timer_enabled, cfg->log_level, cfg->server_ip, cfg->server_port, cfg->max_connections); return 0; }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。