linux c语言 readline,Linux C代码实现读取配置文件示例
/**
读配置文件示例
注:配置文件必须为unix格式,即\n结尾
*/
#include
#include
#include
#include
#include
// 结构体
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 value
conf->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;
}
linux c语言 readline,Linux C代码实现读取配置文件示例相关推荐
- Linux C代码实现读取配置文件示例
最近在看hostapd,该程序使用了conf配置文件,本文参考hostapd代码抽取出读取配置文件的示例,由于配置选项和业务密切相关,实际使用中仍需要做修改. 下面是代码示例: /** 读配置文件示例 ...
- 如何在java代码中读取配置文件
在日常开发过程中,我们经常需要拼接一些字符串之类的东西,而这些字符串往往是不变的,或者在java代码中多次使用到的.当然我们可以在java代码中写死,但是这样做的缺点也是有目共睹的,一旦业务需求发生变 ...
- linux c语言 ppt,Linux下C语言编程.ppt
Linux下C语言编程 Linux中C语言的重要性 Linux和C天生有不解之缘. Linux操作系统的内核主要是用C写的,另外Linux下的很多软件也是用C写的,特别是一些著名的服务软件,比如MyS ...
- linux c语言 密码,Linux 双因子认证(密码+PIN)C语言版
Linux 双因子认证(密码+PIN)C语言版.md 关键字 Linux C语言 PAM SSH 2 Two Multi Factor Authentication Login 双因子 多因子 密保 ...
- 限速linux c语言,基于Linux系统的流量控制程序的C语言代码
基于Linux系统的流量控制程序的C语言代码 基于Linux系统的流量控制程序的C语言代码 mytbf.h头文件 ifndef MYTBF_H_ #define MYTBF_H_ typedef vo ...
- linux泰语语言包,linux安装中文语言包(示例代码)
相关配置如下: yum install fonts-chinese.noarch yum install m17n-db-common-cjk yum install m17n-db-chinese ...
- linux c语言 ppt,linux操作系统下c语言编程入门.ppt
linux操作系统下c语言编程入门.ppt Linux操作系统下C语言编程入门 CNT Linux操作系统简介基础知识进程介绍文件操作时间概念消息管理线程操作网络编程Linux下C开发工具介绍 一 L ...
- linux c语言工具,Linux下C语言编程环境的工具.doc
Linux下C语言编程环境的工具 Linux下C语言编程环境的工具 Linux下C语言编程环境的工具 要想在Linux下进行C语言编程,首先得搭建好一个编程环境.这里分别说明一下几个非常有用的软件包. ...
- linux c语言 信号,linux下基于C语言的信号编程实例
搜索热词 本文实例讲述了linux下基于C语言的信号编程方法.分享给大家供大家参考.具体如下: #include #include #include #include #include void si ...
最新文章
- 闽南科技学院计算机分数线,2015年闽南科技学院美术专业录取分数线
- MFC 关于new出一个新对话框时,退出对话框内存泄漏的问题解决
- camera客观测试_光学图像测试之屏幕色彩管理测试
- mongotemplate 查list_mongoTemplate.aggregate()聚合查询 时间
- 清华姚班毕业生不配自信?张昆玮在豆瓣征女友,却被网友群嘲......
- [剑指offer]面试题15:链表中倒数第k个结点
- Android NDK之静态/动态注册Native方法
- MANet:盲图像超分辨率中空间变异核估计的互仿射网络(ICCV 2021)
- react-native ListView 封装 实现 下拉刷新/上拉加载更多
- 30岁软件测试转产品_30岁的我脸上竟然出现皱纹了,求推荐抗衰老产品,最好是国外的...
- mysql的配置以及后端数据库的连接
- js 获取对象属性个数
- BUG Error:Execution failed for task ':app:dexDebug'.
- Math详解大全,数学类
- 哪些思维方式是你刻意训练过的?
- SSM框架整合+简单的增删改查的实现
- 北师大有计算机技术专硕嘛,北师大新增一硕士专业 称要培养作家
- 数字电路设计的基本方法有哪些
- linux硬盘的两个分区合并_linux新加硬盘合并
- es - elasticsearch - aggs - metrics - percentile_ranks
热门文章
- Centos7.5源码编译安装nodejs
- 信号处理中数字频率和模拟频率简明讲解
- SPI通信总线-51
- C++ 输出当前所在的路径
- python打开文件并读取2进制_在Python中,哪些文件可以读取为文本,哪些文件可以读取为二进制?...
- volatile、synchronized、(原子、可见、有序)、先行发生原则
- java函数名没有加throw_C++函数声明后面加throw()的作用
- linux下system函数的深入理解
- OS / Linux / SIGCHLD 信号
- MySQL / 基本架构介绍