详细介绍了Linux下配置文件的读取方法及fgyhtrh读取操作的C代码实现
来源:互联网 发布:中庸好学力行知耻翻译 编辑:程序博客网 时间:2024/05/22 13:08
Linux具有免费、可靠、安全、稳定、多平台等特点,因此深受广大程序员的欢迎。
为了体现软件产品的灵活性,可添加配置文件存放某些重要的参数,在部署的时候根据实际的安装环境对每个配置项的值进行设置。这就要求程序能够准确读取到各个配置项的值。
本文详细介绍了Linux下配置文件的读取方法及读取操作的C代码实现,为相关的软件开发工作的开展提供了有益的参考。
配置文件介绍
为了便于程序处理,对配置文件的命名及内容格式有一些约定,具体如下:
第一,配置文件的后缀为ini,如本文中使用到的配置文件为:Config.ini。
第二,配置文件的内容由段名、注释和配置项组成,其中,段名由[]括起来,注释的内容以分号(;)开头,配置项采用等号(=)进行赋值。
本文使用的配置文件“Config.ini”包含的具体内容如下:
[EMPLOYEEINFO]
;the name of employee
EmployeeName=wang
;the age of employee
EmployeeAge=25
[EMPLOYERINFO]
;the name of employer
EmployerName=zhou
;the age of employer
EmployerAge=38
三、配置文件读取操作总体流程
实现配置文件读取操作的程序流程如图1所示。
这里写图片描述
图1 配置文件读取操作程序流程
四、配置文件读取操作重要流程
1.获取配置文件的全路径
在本文中,配置文件存放的全路径为:/home/zhou/zhouzx/GetConfig/ Config.ini。实现获取配置文件的全路径的程序函数为GetCompletePath(具体代码见后)。
说明:
(1) 函数getenv用来获取某参数的环境变量的内容。getenv(“HOME”)用于获取程序所在的当前用户的全路径。例如,本程序放在了zhou用户下,那么getenv(“HOME”)的值就为“/home/zhou”。
(2) Linux下目录之间的分隔符为“/”,这个与Windows下的分隔符有区别。
2.匹配段名和配置项名,并获取配置项的值
程序首先找到段名,然后在该段之下去匹配配置项名,最后获取配置项的值。
程序流程如图2所示。
这里写图片描述
图2 获取配置项值的程序流程
实现该功能的程序函数为GetStringContentValue(具体代码见后)。
对配置文件读取操作的测试
为了对编写的配置文件读取操作程序进行测试,定义了员工信息结构体和雇主信息结构体,分别用于存放从配置文件中读取到的员工信息和雇主信息。在main函数中将获取到的信息打印出来,以此来检查程序操作的正确性。
六、C程序实现
本程序命名为“GetConfig.c”,具体代码如下:
/**********************************************************************
* 版权所有 (C)2015, Zhou Zhaoxiong。
*
* 文件名称:GetConfig.c
* 文件标识:无
* 内容摘要:演示Linux下配置文件的读取方法
* 其它说明:无
* 当前版本:V1.0
* 作 者:Zhou Zhaoxiong
* 完成日期:20150507
*
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 数据类型重定义
typedef unsigned char UINT8;
typedef signed int INT32;
typedef unsigned int UINT32;
// 员工信息结构体定义
typedef struct
{
UINT8 szEmployeeName[128]; // 员工姓名
INT32 iEmployeeAge; // 员工年龄
} T_EmployeeInfo;
// 雇主信息结构体定义
typedef struct
{
UINT8 szEmployerName[128]; // 雇主姓名
INT32 iEmployerAge; // 雇主年龄
} T_EmployerInfo;
// 函数声明
void GetCompletePath(UINT8 *pszConfigFileName, UINT8 *pszWholePath);
void GetStringContentValue(FILE *fp, UINT8 *pszSectionName, UINT8 *pszKeyName, UINT8 *pszOutput, UINT32 iOutputLen);
void GetConfigFileStringValue(UINT8 *pszSectionName, UINT8 *pszKeyName, UINT8 *pDefaultVal, UINT8 *pszOutput, UINT32 iOutputLen, UINT8 *pszConfigFileName);
INT32 GetConfigFileIntValue(UINT8 *pszSectionName, UINT8 *pszKeyName, UINT32 iDefaultVal, UINT8 *pszConfigFileName);
INT32 main();
/**********************************************************************
* 功能描述:主函数
* 输入参数:无
* 输出参数:无
* 返 回 值:无
* 其它说明:无
* 修改日期 版本号 修改人 修改内容
* ---------------------------------------------------------------
* 20150507 V1.0 Zhou Zhaoxiong 创建
***********************************************************************/
INT32 main()
{
T_EmployeeInfo tEmployeeInfo = {0};
T_EmployerInfo tEmployerInfo = {0};
// 获取并打印员工信息
// 获取员工姓名
GetConfigFileStringValue("EMPLOYEEINFO", "EmployeeName", "", tEmployeeInfo.szEmployeeName, sizeof(tEmployeeInfo.szEmployeeName), "Config.ini");
http://www.howfile.com/file/b362896/19dfca1a/
http://www.howfile.com/file/b362896/4c90d37e/
http://www.howfile.com/file/b362896/386da012/
http://www.howfile.com/file/b362896/70de50bc/
http://www.howfile.com/file/b362896/0abd8dd3/
http://www.howfile.com/file/b362896/7e0e134d/
http://www.howfile.com/file/b362896/d0035e44/
http://www.howfile.com/file/b362896/2e03c9d2/
http://www.howfile.com/file/b362896/2a2565fa/
http://www.howfile.com/file/b362896/f8ab4352/
http://www.howfile.com/file/b362896/047225ea/
http://www.howfile.com/file/b362896/7f36c79e/
http://www.howfile.com/file/b362896/ff88233c/
http://www.howfile.com/file/b362896/93cc2d18/
http://www.howfile.com/file/b362896/700cf93d/
http://www.howfile.com/file/b362896/75cdc714/
http://www.howfile.com/file/b362896/53752187/
http://www.howfile.com/file/b362896/3bb8a26c/
http://www.howfile.com/file/b362896/d85d1a87/
http://www.howfile.com/file/b362896/6feecb40/
http://www.howfile.com/file/b362896/2220a065/
http://www.howfile.com/file/b362896/78e7ba68/
http://www.howfile.com/file/b362896/37878e98/
http://www.howfile.com/file/b362896/13cde5f2/
http://www.howfile.com/file/b362896/5728a02d/
http://www.howfile.com/file/b362896/f60a197a/
http://www.howfile.com/file/b362896/2bc58ab3/
http://www.howfile.com/file/b362896/1fa1a5cc/
http://www.howfile.com/file/b362896/e5454c29/
http://www.howfile.com/file/b362896/749e1fba/
http://www.howfile.com/file/b362896/18db132b/
http://www.howfile.com/file/b362896/8208996f/
http://www.howfile.com/file/b362896/3794952f/
http://www.howfile.com/file/b362896/42e20952/
http://www.howfile.com/file/b362896/efeb80ff/
http://www.howfile.com/file/b362896/67d64e34/
http://www.howfile.com/file/b362896/3e7fa38c/
http://www.howfile.com/file/b362896/eb3a5aee/
http://www.howfile.com/file/b362896/3efdc1f4/
http://www.howfile.com/file/b362896/966473f3/
http://www.howfile.com/file/b362896/705610ff/
http://www.howfile.com/file/b362896/1c13178c/
http://www.howfile.com/file/b362896/0f503003/
http://www.howfile.com/file/b362896/ed66a279/
http://www.howfile.com/file/b362896/e844bb9a/
http://www.howfile.com/file/b362896/3c71fd18/
http://www.howfile.com/file/b362896/bc92fd1b/
http://www.howfile.com/file/b362896/1c6beb77/
http://www.howfile.com/file/b362896/d8f7b711/
http://www.howfile.com/file/b362896/f9a3541f/
http://howfile.com/file/dfhntu/c8f0e1d1/
http://howfile.com/file/dfhntu/4c5aa46f/
http://howfile.com/file/dfhntu/0621b88f/
http://howfile.com/file/dfhntu/8820e4d1/
http://howfile.com/file/dfhntu/84272862/
http://howfile.com/file/dfhntu/f5925837/
http://howfile.com/file/dfhntu/aea129fb/
http://howfile.com/file/dfhntu/f6c314a5/
http://howfile.com/file/dfhntu/8df2aefe/
http://howfile.com/file/dfhntu/2bc05260/
http://howfile.com/file/dfhntu/3a4948eb/
http://howfile.com/file/dfhntu/d5ddee3d/
http://howfile.com/file/dfhntu/0940e235/
http://howfile.com/file/dfhntu/eb4c6698/
http://howfile.com/file/dfhntu/7c2798a3/
http://howfile.com/file/dfhntu/f5e35847/
http://howfile.com/file/dfhntu/b967cd88/
http://howfile.com/file/dfhntu/945293a8/
http://howfile.com/file/dfhntu/b7cacea1/
http://howfile.com/file/dfhntu/06f51239/
http://howfile.com/file/dfhntu/a68a6f45/
http://howfile.com/file/dfhntu/8d66a516/
http://howfile.com/file/dfhntu/e19794ec/
http://howfile.com/file/dfhntu/ab1fbd06/
http://howfile.com/file/dfhntu/59a43c32/
http://howfile.com/file/dfhntu/f01f3514/
http://howfile.com/file/dfhntu/521f573d/
http://howfile.com/file/dfhntu/653b0e05/
http://howfile.com/file/dfhntu/a3c0df43/
http://howfile.com/file/dfhntu/4dc622ea/
http://howfile.com/file/dfhntu/dba4249d/
http://howfile.com/file/dfhntu/4062cc6e/
http://howfile.com/file/dfhntu/d454285f/
http://howfile.com/file/dfhntu/813f20f0/
http://howfile.com/file/dfhntu/dcb93f2b/
http://howfile.com/file/dfhntu/bc2dcf05/
http://howfile.com/file/dfhntu/2b26ce07/
http://howfile.com/file/dfhntu/411030d4/
http://howfile.com/file/dfhntu/347cbeaf/
http://howfile.com/file/dfhntu/4be4cd6b/
http://howfile.com/file/dfhntu/84d56cec/
http://howfile.com/file/dfhntu/f623342e/
http://howfile.com/file/dfhntu/ab0b61f2/
http://howfile.com/file/dfhntu/f5ec23fa/
http://howfile.com/file/dfhntu/d712675d/
http://howfile.com/file/dfhntu/bc8e6606/
http://howfile.com/file/dfhntu/a9721943/
http://howfile.com/file/dfhntu/1b3aa22c/
http://howfile.com/file/dfhntu/b69e27cc/
http://howfile.com/file/dfhntu/2545b8f8/
http://howfile.com/file/dfhntu/07d5c46d/
http://howfile.com/file/dfhntu/bdcdf77f/
http://howfile.com/file/dfhntu/0ab2c3ca/
http://howfile.com/file/dfhntu/f5d84275/
http://howfile.com/file/dfhntu/5273d567/
http://howfile.com/file/dfhntu/05330d34/
http://howfile.com/file/dfhntu/09a39e64/
http://howfile.com/file/dfhntu/0c3a2d60/
http://howfile.com/file/dfhntu/eac76ef7/
http://howfile.com/file/dfhntu/c7bfffda/
http://howfile.com/file/dfhntu/a6f90631/
http://howfile.com/file/dfhntu/6d8b6016/
http://howfile.com/file/dfhntu/8e72a565/
http://howfile.com/file/dfhntu/960a17bd/
http://howfile.com/file/dfhntu/ad4f205b/
http://howfile.com/file/dfhntu/23d50ff9/
http://howfile.com/file/dfhntu/b2fe96cc/
http://howfile.com/file/dfhntu/ca41b53c/
http://howfile.com/file/dfhntu/c8bd430d/
http://howfile.com/file/dfhntu/2800e56c/
http://howfile.com/file/dfhntu/4a217f66/
http://howfile.com/file/dfhntu/ffa930d4/
http://howfile.com/file/dfhntu/b12b4682/
http://howfile.com/file/dfhntu/77b6da30/
http://howfile.com/file/dfhntu/378a6c9b/
http://howfile.com/file/dfhntu/7ed57e23/
http://howfile.com/file/dfhntu/f5bd3473/
http://howfile.com/file/dfhntu/2d08d727/
http://howfile.com/file/dfhntu/d403cd8f/
http://howfile.com/file/dfhntu/20f599a5/
http://howfile.com/file/dfhntu/f0993683/
http://howfile.com/file/dfhntu/655ac8c3/
http://howfile.com/file/dfhntu/5dfe25fe/
http://howfile.com/file/dfhntu/68dc27c2/
http://howfile.com/file/dfhntu/367da928/
http://howfile.com/file/dfhntu/08d60d3c/
http://howfile.com/file/dfhntu/f370123a/
http://howfile.com/file/dfhntu/cb12bdf6/
http://howfile.com/file/dfhntu/f2254a33/
http://howfile.com/file/dfhntu/64bfeb54/
http://howfile.com/file/dfhntu/21da9e88/
http://howfile.com/file/dfhntu/5d32db98/
http://howfile.com/file/dfhntu/f83fe6ae/
http://howfile.com/file/dfhntu/10034db5/
http://howfile.com/file/dfhntu/729fd3c0/
http://howfile.com/file/dfhntu/141ba207/
http://www.howfile.com/file/jhfdjg/b11eaea8/
http://www.howfile.com/file/jhfdjg/9baddc8f/
http://www.howfile.com/file/jhfdjg/7996c765/
http://www.howfile.com/file/jhfdjg/f02dfee1/
http://www.howfile.com/file/jhfdjg/d368f725/
http://www.howfile.com/file/jhfdjg/0712acb7/
http://www.howfile.com/file/jhfdjg/0557e37c/
http://www.howfile.com/file/jhfdjg/f6532170/
http://www.howfile.com/file/jhfdjg/32345385/
http://www.howfile.com/file/jhfdjg/c8b8cf65/
http://www.howfile.com/file/jhfdjg/2554926c/
http://www.howfile.com/file/jhfdjg/f0de7aed/
http://www.howfile.com/file/jhfdjg/0863c655/
http://www.howfile.com/file/jhfdjg/85ee9e5c/
http://www.howfile.com/file/jhfdjg/0c141234/
http://www.howfile.com/file/jhfdjg/9bb653cc/
http://www.howfile.com/file/jhfdjg/37360e3f/
http://www.howfile.com/file/jhfdjg/f689ab81/
http://www.howfile.com/file/jhfdjg/ab95f26f/
http://www.howfile.com/file/jhfdjg/a5d6b258/
http://www.howfile.com/file/jhfdjg/cca62221/
http://www.howfile.com/file/jhfdjg/87c8b8fc/
http://www.howfile.com/file/jhfdjg/e5e7b22c/
http://www.howfile.com/file/jhfdjg/25cfb8a8/
http://www.howfile.com/file/jhfdjg/da4b6fc8/
http://www.howfile.com/file/jhfdjg/1f056d9d/
http://www.howfile.com/file/jhfdjg/e25e945c/
http://www.howfile.com/file/jhfdjg/3362c1d9/
http://www.howfile.com/file/jhfdjg/4d56c42e/
http://www.howfile.com/file/jhfdjg/663e2af1/
http://www.howfile.com/file/jhfdjg/aa629efe/
http://www.howfile.com/file/jhfdjg/ff7813a7/
http://www.howfile.com/file/jhfdjg/c7821f49/
http://www.howfile.com/file/jhfdjg/950d21ba/
http://www.howfile.com/file/jhfdjg/c80546ed/
http://www.howfile.com/file/jhfdjg/0c998ba2/
http://www.howfile.com/file/jhfdjg/61b591b2/
http://www.howfile.com/file/jhfdjg/7a9277d3/
http://www.howfile.com/file/jhfdjg/9d20274a/
http://www.howfile.com/file/jhfdjg/cf65ab99/
http://www.howfile.com/file/jhfdjg/2b5b975d/
http://www.howfile.com/file/jhfdjg/cb89510f/
http://www.howfile.com/file/jhfdjg/dc93d073/
http://www.howfile.com/file/jhfdjg/5e69acba/
http://www.howfile.com/file/jhfdjg/84e094b0/
http://www.howfile.com/file/jhfdjg/4f915ee3/
http://www.howfile.com/file/jhfdjg/120bc36c/
http://www.howfile.com/file/jhfdjg/1f4898f5/
http://www.howfile.com/file/jhfdjg/8f96c7a0/
http://www.howfile.com/file/jhfdjg/82223138/
http://www.howfile.com/file/jhfdjg/a3369166/
http://www.howfile.com/file/jhfdjg/e54ede05/
http://www.howfile.com/file/jhfdjg/3bebfd65/
http://www.howfile.com/file/jhfdjg/efb5ea80/
http://www.howfile.com/file/jhfdjg/d73d9c27/
http://www.howfile.com/file/jhfdjg/6d08ab99/
http://www.howfile.com/file/jhfdjg/bc14cf3d/
http://www.howfile.com/file/jhfdjg/d9114613/
http://www.howfile.com/file/jhfdjg/045eaff9/
http://www.howfile.com/file/jhfdjg/1a6cab60/
http://www.howfile.com/file/jhfdjg/920d0a16/
http://www.howfile.com/file/jhfdjg/10f4db8a/
http://www.howfile.com/file/jhfdjg/f5485c63/
- 详细介绍了Linux下配置文件的读取方法及fgyhtrh读取操作的C代码实现
- Linux下配置文件读取操作流程及其C代码实现
- Linux下配置文件读取操作流程及其C代码实现
- 【转】Linux下配置文件读取操作流程及其C代码实现
- Linux C代码实现读取配置文件示例
- 分享一个linux下的C程序读取配置文件的方法
- ini配置文件的格式及读取操作
- 配置文件的读取,纯C代码
- 配置文件读取的简要C/C++代码
- 读取配置文件的方法
- linux读取自定义的配置文件方法
- linux中读取配置文件的方法
- Linux下读取磁盘硬盘容量大小的C代码实现,不用df -h命令
- 通用读取配置文件的代码
- 读取配置文件的内容代码
- Linux下用C读取INI配置文件
- Linux下用C语言读取配置文件
- Linux下用C读取INI配置文件
- ObjectiveC中setter getter 方法初体验
- ObjectiveC中关于description函数
- Eclipse 反编译 JD-Eclipse 插件安装
- cpu读写数据的大致流程
- weka实战004:fp-growth关联规则算法
- 详细介绍了Linux下配置文件的读取方法及fgyhtrh读取操作的C代码实现
- 窗体透明、窗体上控件和DC绘图不透明
- U盘刻录GSMS软件系统ISO文件教程
- 可重入与线程安全
- ALE 配置和操作教程说明
- MongoDB 3.0 安装笔记
- 单例模式的常见应用场景
- 奇酷手机曝光
- JTable的清空小技巧以及JTable的详细介绍