最近在看hostapd,该程序使用了conf配置文件,本文参考hostapd代码抽取出读取配置文件的示例,由于配置选项和业务密切相关,实际使用中仍需要做修改。

下面是代码示例:

/**
读配置文件示例
注:配置文件必须为unix格式,即\n结尾*/#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>// 结构体
struct hostapd_config
{char iface[16];uint16_t beacon_int;uint8_t channel;char country[3]; /* first two octets: country code as described in* ISO/IEC 3166-1. Third octet:* ' ' (ascii 32): all environments* 'O': Outdoor environemnt only* 'I': Indoor environment only*/
};// 默认值
struct hostapd_config * config_defaults(void)
{struct hostapd_config *conf = NULL;conf = (struct hostapd_config *)calloc(1, sizeof(*conf));if (conf == NULL){return NULL;}// set default valueconf->beacon_int = 100;conf->channel = 1;return conf;
}// 根据读取到的信息填充结构体
static int config_defaults(struct hostapd_config *conf, const char *buf, char *pos, int line)
{if (strcmp(buf, "interface") == 0){strncpy(conf->iface, pos, sizeof(conf->iface));}else if (strcmp(buf, "channel") == 0){int val = atoi(pos);conf->channel = val;}else if (strcmp(buf, "beacon_int") == 0){int val = atoi(pos);conf->beacon_int = val;}else if (strcmp(buf, "country_code") == 0){memcpy(conf->country, pos, 2);conf->country[2] = ' ';}// more ...return 0;
}//
// 读配置文件
struct hostapd_config* config_read(const char *fname)
{struct hostapd_config *conf = NULL;FILE *f = NULL;char buf[1024] = {0};char *pos = NULL;int line = 0;int errors = 0;f = fopen(fname, "r");if (f == NULL){printf("Could not open configuration file '%s' for reading.\n", fname);return NULL;}conf = config_defaults();if (conf == NULL){fclose(f);return NULL;}while (fgets(buf, sizeof(buf), f)){line++;if (buf[0] == '#')continue;pos = buf;while (*pos != '\0'){if (*pos == '\n'){*pos = '\0';break;}pos++;}if (buf[0] == '\0')continue;pos = strchr(buf, '=');if (pos == NULL) {printf("Line %d: invalid line '%s'\n", line, buf);errors++;continue;}*pos = '\0';pos++;errors += config_defaults(conf, buf, pos, line);}fclose(f);return conf;
}/int main(int argc, char *argv[])
{const char* conf_file = "foo.conf";if (argc == 2){conf_file = argv[1];}struct hostapd_config *conf = NULL;conf = config_read(conf_file);if (conf == NULL){printf("failed to read file: %s\n", conf_file);return -1;}printf("%d %s %s\n", conf->channel, conf->country, conf->iface);return 0;
}

李迟 2016.8.21 周日

Linux C代码实现读取配置文件示例相关推荐

  1. linux c语言 readline,Linux C代码实现读取配置文件示例

    /** 读配置文件示例 注:配置文件必须为unix格式,即\n结尾 */ #include #include #include #include #include // 结构体 struct host ...

  2. 如何在java代码中读取配置文件

    在日常开发过程中,我们经常需要拼接一些字符串之类的东西,而这些字符串往往是不变的,或者在java代码中多次使用到的.当然我们可以在java代码中写死,但是这样做的缺点也是有目共睹的,一旦业务需求发生变 ...

  3. 当前工程读取配置文件示例

    当前工程目录读取配置示例: public static void main(String[] args) {// TODO Auto-generated method stub System.out. ...

  4. SpringBoot 读取配置文件中参数全面教程

    一.简介 在日常开发使用 SpringBoot 框架时,经常有一些配置信息需要放置到配置文件中,我们需要手动读取这些配置到应用中进行一些逻辑,这里整理了一些常用读取配置的方法,简单介绍一下. 1.Sp ...

  5. python读取配置文件失败原因_python读取配置文件报keyerror-文件路径不正确导致的错误...

    - 在其他模块使用反射读取配置文件报错,但是在反射模块中读取GetData.check_list又是正确的 反射模块如下: # get_data.py from API_AUTO.p2p_projec ...

  6. c++读取txt文件中的数字_SpringBoot 多种读取配置文件中参数的方式

    点击上方☝SpringForAll社区 轻松关注!及时获取有趣有料的技术文章 本文来源:http://www.mydlq.club/article/61/ . 一.简介 . 1.SpringBoot ...

  7. 根据不同的操作系统读取配置文件/java读取属性文件代码

    package cn.com.css.common.util; /**  * @brief OSEnum.java 操作系统的枚举  * @attention  * @author 涂作权  * @d ...

  8. php+怎么读配置文件的内容,php读取配置文件的代码一例

    本节内容: php读取配置文件 1,配置文件格式: # # Main Configuration File # # it can be default or whatever language. On ...

  9. SpringBoot - application.yml配置文件中yes/no,on/off在代码中读取的值为true/false

    写在前面 在SpringBoot的项目中,当在配置文件中配置的值为yes/no或者on/off时,在SpringBoot内部解析时会将yes/no,on/off解析为true/false. 参数配置 ...

最新文章

  1. 2022-2028年中国磷肥工业投资分析及前景预测报告
  2. mysql二进制日志文件差不多_mysql数据同步-基于二进制日志文件和position复制点的方式...
  3. 被Html的Button标签耍了一次
  4. 19、Power Query-快速分析各产品的销售情况
  5. 博士申请 | 加拿大Mila实验室唐建教授招收深度学习方向博士生和实习生
  6. SAP Spartacus 的页面布局
  7. 微信小程序echarts层级太高
  8. android fragment动画,Fragment 的过场动画
  9. 不同公式等号对齐_数学公式编辑器“制作amp;套用公式模板”和“文本对齐问题”的完美解决方案...
  10. ospf多区域路由实验
  11. 驰声科技推出AI对话考辅系统 将VR引入语音评测
  12. oracle 删除用户和依赖,Oracle 12.2使用手动创建与注册依赖对象来执行联机重定义...
  13. SpringBoot实战之文件上传微软云(Azure Storage)
  14. SQL 必知必会·笔记6使用数据处理函数
  15. 设置 Scite编辑器的默认编辑为 UTF-8 ,及其他一些参数配置
  16. CC00009.CloudOpenStack——|OpenStack组件.V02|——|openstack-glance|controller节点下部署glanc
  17. 为什么说买腾讯云服务器通过代理商购买更划算
  18. lerna 常用命令
  19. Maven下载、安装与配置
  20. CSS 选择器及常用属性介绍

热门文章

  1. 孙宏斌,真的押上了全部身家?
  2. 智商税?苹果1.8米连接线卖949元、3米的1169元被吐槽:高攀不起
  3. 祝福神器!微信推出“表情搜索”功能,支持合成个性化祝福表情
  4. 联想拯救者Y90游戏性能实测:全程满帧 散热能力出众
  5. 跟苹果在一起:腾讯音乐与Apple Music达成全球合作
  6. 英国反垄断监管机构将对音乐流媒体市场展开调查
  7. 比尔 · 盖茨即将在美国商会发表演讲 系离婚后首次亮相
  8. 疑似全新华为Mate X2折叠屏手机入网:麒麟9000重磅加持
  9. iPhone 12 Pro/Pro Max最新渲染图曝光
  10. 坐地起价、山寨横行、人身骚扰:割韭菜的刀,还是“搬家公司”快