C载入txt文本打开文件配置文件

文本装入Config的一个快速方法。模版,稍加更改后可装入使用。

#include<public.hpp>

void Fill_config(char *option,char *property,StateBuffer *statebuffer)
{
    string option_buffer(option);
    string property_buffer(property);
    if(option_buffer=="ipaddr")
    {
        statebuffer->data_origin = property_buffer ;
        statebuffer->data_origin.erase(statebuffer->data_origin.find_first_of("'"),1);
        statebuffer->data_origin.erase(statebuffer->data_origin.find_last_of("'"),1);
    }
    else if(option_buffer=="Error_Retrans_Enable")
    {
        statebuffer->Error_Retrans_Enable=0 ;
        if(property_buffer=="'1'"){
            statebuffer->Error_Retrans_Enable =1 ;
        }
    }
    else{
        cout<<"message bad format"<<endl;
    }
}

int LoadConfigtxt()
{
 char line[60];
 char option[30], property[30],buffer[15];

 FILE *fp = fopen("/etc/config/Config","a+");
 if(fp==NULL)
 {
     printf("Config text black !!! ");
     return 0 ;
 }

 while(!feof(fp))//end return ture
 {
    memset(line,0,60);  // clean memory line
    memset(option,0,30); // clean memory name
    memset(property,0,30);		// clean ..
    fgets(line,60,fp);  // read 50 chars frome fp to line untill \t or end of file

    sscanf(line, "%s%s%s", buffer,option, property);  // format input keyword space
    //printf("%s\n",line);
    printf("option:%s, property:%s\n",option,property);
    Fill_config(option,property,&statebuffer);
 }
 return 1 ;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值