**C语言通过定义结构体UCI读写配置文件

1、配置文件名称:roaming
#define UCI_ROAM_FILE "/etc/config/roaming" //配置文件
config wlan "wlan0"
option basicnetwork_name 'siot'
option basicstate 'enable'
option securitysuite 'wpa2'
option securitywpaxauthentication 'psk'
option securitywpaxieee_80211r 'disabled'
option securitywpaxpassphrase '12345678'

2、需要定义结构体数组

KEY Roaming_Conf_Value[6] ={{"basicnetwork_name",0,0},\
{"basicstate",1,0},\
{"securitysuite",2,0},\
{"securitywpaxauthentication",3,0},\
{"securitywpaxieee_80211r",4,0},\
{"securitywpaxpassphrase",5,0}};

3、然后需要定义UCI对象
static struct uci_context ctx = NULL;
4、然后编写单个写入配置文件函数;
void write_roam_config(char
package_char,char section_char,char roam_conf_name,char roam_conf_value)
{ //写入配置文件roaming
int i=0;
printf("write_roam_config.......start .....\n");
struct uci_package
pkg= NULL;
ctx = uci_alloc_context();

if (UCI_OK != uci_load(ctx, UCI_ROAM_FILE, &pkg))    goto cleanup; //if open false then goto cleanup:char p[100];char s[100];char o[100];char c[100];    sprintf(p,"%s",package_char);   //配置文件名    sprintf(s,"%s",section_char);   //wlan0     sprintf(o,"%s",roam_conf_name); //option 对应的名称如:basicnetwork_name     sprintf(c,"%s",roam_conf_value);//value值: siot    struct uci_ptr ptr ={       .package= p,       .section= s,       .option = o,       .value = c,    };    uci_set(ctx, &ptr); //write config    uci_commit(ctx, &ptr.p, false); //save    uci_unload(ctx, ptr.p); //unload

cleanup:
uci_free_context(ctx);
ctx=NULL;

}
5、读配置文件,给结构体数组赋值函数;
void read_config_init()
{
printf("read_config_init() is begining \n");
struct uci_package pkg = NULL;
struct uci_element
e;

const char *value; ctx = uci_alloc_context(); //  if (UCI_OK != uci_load(ctx, UCI_ROAM_FILE, &pkg))     goto cleanup;  //foreach "power" elementuci_foreach_element(&pkg->sections, e) {    struct uci_section *s = uci_to_section(e);     //print section type    //printf("section s's type is %s.\n",s->type);    //if(!strcmp) if(!strcmp("power",s->type));         for(int i=0;i<=5;i++){             if (NULL != (value = uci_lookup_option_string(ctx,\                    s, Roaming_Conf_Value[i].name))) {             strcpy(Roaming_Conf_Value[i].value,strdup(value));             //printf("%s's %s is %s.\n",s->e.name,Roaming_Conf_Value[i].name,value);            }        }} uci_unload(ctx, pkg);  

cleanup:
uci_free_context(ctx);
ctx = NULL;
printf("read_config_init() is end \n");
}**

©著作权归作者所有:来自51CTO博客作者WX13953208048的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 2021-03-10:一个数组上共有 N 个点,序号为0的点是起点位置,序号为N-
  2. kubernetes常用控制器之Job和CronJob
  3. 五款Python图像处理工具!Python入门分享!
  4. 2021-03-08:在一个数组中,任何一个前面的数a,和任何一个后面的数b,如
  5. Ansible入门之Playbook
  6. 安卓自定义对话框的使用案例
  7. JavaScript初学习/之classList:动态设置元素类、dataset: 读写自
  8. 面试懵了:StringBuilder为什么线程不安全
  9. MySQL的SQL语句 - 数据定义语句 - DROP 语句(3)

随机推荐

  1. 关于C#如何实现Access以时间段查询出来的
  2. WPF实现定时刷新UI界面的实例详解
  3. ASP.NET Core中新功能--环境变量和启动设
  4. 基于Calendar实现blog日历的实例详解
  5. 创建自己的RSS实例教程
  6. MVC5下拉框单选绑定的具体代码实现
  7. Asp.net 中用GridView控件的实例教程
  8. C#中如何使用SendMessage?
  9. .NET Core 2.0 Preview2的详细介绍
  10. MVVM模式下WPF动态展示图片实例分析