linux c 读取配置文件
来源:互联网 发布:淘宝商品存在交易风险 编辑:程序博客网 时间:2024/05/17 06:00
源码如下:
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 2typedef 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;}
0 0
- linux c读取配置文件
- linux c 读取配置文件
- 读取配置文件源代码[linux c]
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- linux c 使用libxml2读取配置文件
- linux编程(C语言读取配置文件)
- Linux下用C读取INI配置文件
- Linux c 配置文件读取与修改
- DDL,DML,DCL示例图
- poj-2079 Triangle
- 【原创】使用棋盘格实现摄像头序号标定
- TD不换行 nowrap属性
- XMPP/Jingle Vs SIP/SIMPLE 的简单介绍
- linux c 读取配置文件
- 二、CXF与springMVC整合的webService客户端调用
- annotation
- 有 JavaScript 基础,怎么学习 Node.js
- 《弄潮》读后感
- Uva 1632 alibaba 区间dp
- 如何修改pdf格式文件
- 后台的Activity被系统回收怎么办
- 分布式架构模型