配置文件的读与写(一)

配置文件的读取与写入总结为如下步骤: 
        1.打开配置文件 ,获取根节点指针。

        2.读取配置文件相关内容。

        3.修改/写入配置文件的相关内容。

        4.关闭配置文件。

下面进行详细的介绍:

前提:

a .所涉及的头文件:
    #include <libxml/tree.h>
    #include <libxml/parser.h>
    #include <libxml/xpath.h>
    #include <libxml/xpathInternals.h>

b.宏定义 

#define  C_CONFIG_DEVICE_MAX_NAME_LEN  20
#define  C_CONFIG_DEVICE_MAX_ADDRESS_LEN 6
#define  file_path   "/home/profile/path/ma_config.xml"

#define NULL  0

 c. 所涉及的枚举类型以及 结构体 

typedef struct{
     unsignec char status;
     uint   time;
     uint   function;
     char   name[C_CONFIG_DEVICE_MAX_NAME_LEN];
     char  address[C_CONFIG_DEVICE_MAX_ADDRESS_LEN];
}ma_config_info_t;

typedef enum{
   C_CONFIG_EVENT_STATUS,
   C_CONFIG_EVENT_TIME,
   C_CONFIG_EVENT_FUNCTION,
   C_CONFIG_EVENT_NAME,
   C_CONFIG_EVENT_ADDRESS,
   C_CONFIG_EVENT_END 
}ma_config_event_e;


typedef enum{
   C_CONFIG_SUCCESS,
   C_CONFIG_FALSE,
   C_CONFIG_ENDS 
}ma_event_result_e;


 c.所涉及的全局变量

   xmlDocPtr   doc =NULL;
   xmlNodePtr  node = NULL;
    ma_config_info_t  ma_config_info; 

 d.函数的声明

int ma_config_file_open(char* file_path);
int ma_config_file_read_file(void);
int ma_config_read(int event_id ,char* data,int data_length);
int ma_config_file_write(int event_id ,char * data,int data_length);
int ma_config_close(void);

static int ma_read_phone_cfg(XmlNodePtr  node);
static int ma_read_video_cfg(XmlNodePtr  node);
static int ma_phone_set_cfg(int event_id ,char* data,int data_length);
static int ma_video_set_cfg(int event_id , char * data,int data_length);

xmlNodePtr ma_get_video_node(void);
xmlNodePtr ma_get_phone_node(void);
static int  ma_string_to_char(char*,char*,uint );
static int  ma_char_to_string(char*,char*,uint );

e 所需的配置文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>   
<config>      
     <phone>   
           <status>0</status>   
           <time>0010</time>
           <function>0100</fuction> 
           <name>huawei</name>  
     </phone>   
     <video>   
           <address>010203040506</address>     
     </video>  
</config>












            



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值