linux c 读取配置文件

源码如下:

config.h

#ifndef CONFIG_H_INCLUDED
#define CONFIG_H_INCLUDED

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>

typedef struct item_t{
    char key[50];
    char value[50];
}ITEM;

/*去除字符串右端空格*/
char *strtrimr(char *pstr);

/*去掉字符串左端空格*/
char *strtriml(char *pstr);

/*去掉字符串的空格*/
char *strtrim(char *pstr);

/*读取配置文件中的一行*/
ITEM *get_item_from_line(char *line,ITEM *item);

/*读取配置文件中所有的配置*/
int get_items_from_file(const char *file, ITEM *items);

/*查找value*/
char *get_value_from_items(const char *key, char *value, ITEM *items, int len);
#endif // CONFIG_H_INCLUDED

config.c

#include "config.h"

//去掉字符串右端空格
char *strtrimr(char *pstr){
    /*从右至左取出每一个空格字符,然后填入字符从结束符*/
    int i = strlen(pstr) - 1;
    if(isspace(pstr[i]) && (i >= 0))
        pstr[i--] = '\0';
     return pstr;
}

//去掉字符串左端空格
char *strtriml(char *pstr){
    /*从左至右判断字符是否时空格,以最后一个空格的下标重新拷贝字符串*/
    int i = 0,j;
    j = strlen(pstr) - 1;
    while(isspace(pstr[i]) && (i <= j))
       ++i;
    if(i > 0)
        strcpy(pstr,&pstr[i]);
    return pstr;
}

//去掉字符串的空格
char *strtrim(char *pstr){
    /*先去除左边然后去除右边*/
    return strtrimr(strtriml(pstr));
}

//读取配置文件中的一行(取出key 和 value)
/*
line 是配置配置文件中的一行
item 用来存放读取后的 key 和 value
*/
ITEM *get_item_from_line(char *line, ITEM *item){
    //去除空格
    char *p = strtrim(line);
    int len = strlen(p);
    if(len <= 0)  //   空行
        return NULL;
    else if(p[0] == '#')  //注释行
        return NULL;
    else{
        char *p2 = strchr(p,'=');   //以 = 号为分割,分别取出 key 和 value
        *p2++ = '\0';
        strcpy(item->key,p);
        strcpy(item->value,p2);
    }
    return item;
}

//读取配置文件中所有的配置
/*
file 配置文件
items  ITEM数组,存放配置文件中的所有 key 和 value
*/
int get_items_from_file(const char *file, ITEM *items){
    int count = 0;
    char line[1024];
    FILE *fp = fopen(file,"r");
    if(fp == NULL)
        return -1;
    while(fgets(line,1023,fp)){
        get_item_from_line(line,&items[count]);
        ++count;
    }
    printf("%d\n",count);
    close(fp);
    return count;
}

/*在 ITEM 数组中 查找 key 对应的 value, 并存入 value参数指向的数组中*/
char *get_value_from_items(const char *key, char *value, ITEM *items, int len){
    int i = 0;
    //value = NULL;
    for(;i < len ; ++i){
        if(!strcmp(items[i].key,key)){
            strcpy(value,items[i].value);
            break;
        }
    }
    return value;
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "config.h"

#define CONFIG_FILE_LINE        2

typedef struct m_conf{
    char serverip[20];
    int port;
}M_CONF;

M_CONF mconfig;



void init_mconfig(void){
    ITEM items[CONFIG_FILE_LINE];
    //char *key[] = {"serverip","port"};
    get_items_from_file("m.conf",items);
    char tmp_buf[50];


    strcpy(mconfig.serverip,get_value_from_items("serverip",tmp_buf,items,CONFIG_FILE_LINE));
    mconfig.port = atoi(get_value_from_items("port",tmp_buf,items,CONFIG_FILE_LINE));

}
int main()
{


    init_mconfig();
    printf("%s\n%d\n",mconfig.serverip,mconfig.port);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值