C 配置文件
*************打開配置文件************
[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
?
/**********************************************************************
?* 版權所有 (C)2015, Zhao Yun。
?*
?* 文件名稱:GetConfig.c
?* 文件標識:無
?* 內容摘要:演示Linux下配置文件的讀取方法
?* 其它說明:無
?* 當前版本:V1.0
?* 作 ? ?者:Zhao yun
?* 完成日期: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 ? ? ? ? ?Zhao Yun ? ? ? ? ? 創建
***********************************************************************/
INT32 main()
{
? ? T_EmployeeInfo tEmployeeInfo = {0};
? ? T_EmployerInfo tEmployerInfo = {0};
?? ?
?? ? // 獲取并打印員工信息
?? ? // 獲取員工姓名
?? ? GetConfigFileStringValue("EMPLOYEEINFO", "EmployeeName", "", tEmployeeInfo.szEmployeeName, sizeof(tEmployeeInfo.szEmployeeName), "/mnt/Config.ini");
?? ?
?? ? // 獲取員工年齡
?? ? tEmployeeInfo.iEmployeeAge = GetConfigFileIntValue("EMPLOYEEINFO", "EmployeeAge", 20, "Config.ini");
? ? if (tEmployeeInfo.iEmployeeAge == -1) ?// 判斷獲取到的年齡是否正確
? ? {
? ? ? ? printf("Get EmployeeAge failed!\n");
? ? ? ? return -1;
? ? }
? ? // 打印讀取到的員工姓名和年齡
? ? printf("EmployeeName is %s, EmployeeAge is %d\n", tEmployeeInfo.szEmployeeName, tEmployeeInfo.iEmployeeAge);
?? ?// 獲取并打印雇主信息
?? ?// 獲取雇主姓名
?? ?GetConfigFileStringValue("EMPLOYERINFO", "EmployerName", "", tEmployerInfo.szEmployerName, sizeof(tEmployerInfo.szEmployerName), "/mnt/Config.ini");
?? ?// 獲取員工年齡
?? ?tEmployerInfo.iEmployerAge = GetConfigFileIntValue("EMPLOYERINFO", "EmployerAge", 30, "Config.ini");
? ? if (tEmployerInfo.iEmployerAge == -1) ?// 判斷獲取到的年齡是否正確
? ? {
? ? ? ? printf("Get EmployerAge failed!\n");
? ? ? ? return -1;
? ? }
?? ?// 打印讀取到的員工姓名和年齡
?? ?printf("EmployerName is %s, EmployerAge is %d\n", tEmployerInfo.szEmployerName, tEmployerInfo.iEmployerAge);?
?? ?return 0; ? ? ? ? ? ? ?
}
/**********************************************************************
* 功能描述: 獲取配置文件完整路徑(包含文件名)
* 輸入參數: pszConfigFileName-配置文件名
?? ? pszWholePath-配置文件完整路徑(包含文件名)
* 輸出參數: 無
* 返 回 值: 無
* 其它說明: 無
* 修改日期 ? ? ? ?版本號 ? ? ? ? 修改人 ? ? ? 修改內容
* ------------------------------------------------------------------
* 20150507 ? ? ? ?V1.0 ? ? ? ? ? Zhao Yun ? ? 創建
********************************************************************/
void GetCompletePath(UINT8 *pszConfigFileName, UINT8 *pszWholePath)
{
? ? UINT8 *pszHomePath ? ? ?= NULL;
? ? UINT8 ?szWholePath[256] = {0};
?? ?
?? ?// 先對輸入參數進行異常判斷
?? ?if (pszConfigFileName == NULL || pszWholePath == NULL)
? ? {
? ? ? ? printf("GetCompletePath: input parameter(s) is NULL!\n");
? ? ? ? return;
? ? }
? ? pszHomePath = (UINT8 *)getenv("HOME"); ? ? // 獲取當前用戶所在的路徑
? ? if (pszHomePath == NULL)
? ? {
? ? ? ? printf("GetCompletePath: Can't find home path!\n");
? ? ? ? return;
? ? }
?? ?
?? ? // 拼裝配置文件路徑
?? ?snprintf(szWholePath, sizeof(szWholePath)-1, "%s/zhouzx/GetConfig/%s", pszHomePath, pszConfigFileName);
? ? strncpy(pszWholePath, szWholePath, strlen(szWholePath));
}
/**********************************************************************
* 功能描述: 獲取具體的字符串值
* 輸入參數: fp-配置文件指針
?? ?pszSectionName-段名, 如: GENERAL
?? ?pszKeyName-配置項名, 如: EmployeeName
??? ?iOutputLen-輸出緩存長度
* 輸出參數: pszOutput-輸出緩存
* 返 回 值: 無
* 其它說明: 無
* 修改日期 ? ? ? ? 版本號 ? ? ? ?修改人 ? ? ? 修改內容
* ------------------------------------------------------------------
* 20150507 ? ? ? ? V1.0 ? ? ? ? ?Zhao yun ? ? 創建
********************************************************************/
void GetStringContentValue(FILE *fp, UINT8 *pszSectionName, UINT8 *pszKeyName, UINT8 *pszOutput, UINT32 iOutputLen)
{
? ? UINT8 ?szSectionName[100] ? ?= {0};
? ? UINT8 ?szKeyName[100] ? ? ? ?= {0};
? ? UINT8 ?szContentLine[256] ? ?= {0};
? ? UINT8 ?szContentLineBak[256] = {0};
? ? UINT32 iContentLineLen ? ? ? = 0;
? ? UINT32 iPositionFlag ? ? ? ? = 0;
? ? // 先對輸入參數進行異常判斷
? ? if (fp == NULL || pszSectionName == NULL || pszKeyName == NULL || pszOutput == NULL)
? ? {
? ? ? ? printf("GetStringContentValue: input parameter(s) is NULL!\n");
? ? ? ? return;
? ? }
? ? sprintf(szSectionName, "[%s]", pszSectionName);
? ? strcpy(szKeyName, pszKeyName);
? ? while (feof(fp) == 0)
? ? {
? ? ? ? memset(szContentLine, 0x00, sizeof(szContentLine));
? ? ? ? fgets(szContentLine, sizeof(szContentLine), fp); ? ? ?// 獲取段名
? ? ? ? // 判斷是否是注釋行(以;開頭的行就是注釋行)或以其他特殊字符開頭的行
? ? ? ? if (szContentLine[0] == ';' || szContentLine[0] == '\r' || szContentLine[0] == '\n' || szContentLine[0] == '\0')
? ? ? ? {
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? // 匹配段名
? ? ? ? if (strncasecmp(szSectionName, szContentLine, strlen(szSectionName)) == 0) ? ??
? ? ? ? {
? ? ? ? ? ? while (feof(fp) == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? memset(szContentLine, ? ?0x00, sizeof(szContentLine));
? ? ? ? ? ? ? ? memset(szContentLineBak, 0x00, sizeof(szContentLineBak));
? ? ? ? ? ? ? ? fgets(szContentLine, sizeof(szContentLine), fp); ? ? // 獲取字段值
? ? ? ? ? ? ? ? // 判斷是否是注釋行(以;開頭的行就是注釋行)
? ? ? ? ? ? ? ? if (szContentLine[0] == ';')
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? memcpy(szContentLineBak, szContentLine, strlen(szContentLine));
? ? ? ? ? ? ? ? // 匹配配置項名
? ? ? ? ? ? ? ? if (strncasecmp(szKeyName, szContentLineBak, strlen(szKeyName)) == 0) ? ??
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? iContentLineLen = strlen(szContentLine);
? ? ? ? ? ? ? ? ? ? for (iPositionFlag = strlen(szKeyName); iPositionFlag <= iContentLineLen; iPositionFlag ++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (szContentLine[iPositionFlag] == ' ')
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? if (szContentLine[iPositionFlag] == '=')
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? iPositionFlag = iContentLineLen + 1;
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? iPositionFlag = iPositionFlag + 1; ? ?// 跳過=的位置
? ? ? ? ? ? ? ? ? ? if (iPositionFlag > iContentLineLen)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? memset(szContentLine, 0x00, sizeof(szContentLine));
? ? ? ? ? ? ? ? ? ? strcpy(szContentLine, szContentLineBak + iPositionFlag);
? ? ? ? ? ? ? ? ? ? // 去掉內容中的無關字符
? ? ? ? ? ? ? ? ? ? for (iPositionFlag = 0; iPositionFlag < strlen(szContentLine); iPositionFlag ++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (szContentLine[iPositionFlag] == '\r' || szContentLine[iPositionFlag] == '\n' || szContentLine[iPositionFlag] == '\0')
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? szContentLine[iPositionFlag] = '\0';
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? // 將配置項內容拷貝到輸出緩存中
? ? ? ? ? ? ? ? ? ? strncpy(pszOutput, szContentLine, iOutputLen-1);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (szContentLine[0] == '[')
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? }
? ? }
}
/**********************************************************************
* 功能描述: 從配置文件中獲取字符串
* 輸入參數: pszSectionName-段名, 如: GENERAL
?? ??? ??? ?pszKeyName-配置項名, 如: EmployeeName
?? ??? ??? ?pDefaultVal-默認值
?? ??? ??? ?iOutputLen-輸出緩存長度
?? ??? ??? ?pszConfigFileName-配置文件名
* 輸出參數: pszOutput-輸出緩存
* 返 回 值: 無
* 其它說明: 無
* 修改日期 ? ? ? ?版本號 ? ? ? ? 修改人 ? ? ? 修改內容
* ------------------------------------------------------------------
* 20150507 ? ? ? ?V1.0 ? ? ? ? ? Zhao Yun ? ? 創建
********************************************************************/?
void GetConfigFileStringValue(UINT8 *pszSectionName, UINT8 *pszKeyName, UINT8 *pDefaultVal, UINT8 *pszOutput, UINT32 iOutputLen, UINT8 *pszConfigFileName)
{
? ? FILE ?*fp ? ? ? ? ? ? ? ? ? ?= NULL;
? ? //UINT8 ?szWholePath[256] ? ? ?= {0};
? ? // 先對輸入參數進行異常判斷
?? ? if (pszSectionName == NULL || pszKeyName == NULL || pszOutput == NULL || pszConfigFileName == NULL)
? ? {
? ? ? ? printf("GetConfigFileStringValue: input parameter(s) is NULL!\n");
? ? ? ? return;
? ? }
? ? // 獲取默認值
?? ?if (pDefaultVal == NULL)
? ? {
? ? ? ? strcpy(pszOutput, "");
? ? }
? ? else
? ? {
? ? ? ? strcpy(pszOutput, pDefaultVal);
? ? }
? ? // 打開配置文件
?? ?//GetCompletePath(pszConfigFileName, szWholePath);
? ? fp = fopen(pszConfigFileName, "r");
? ? if (fp == NULL)
? ? {
? ? ? ? //printf("GetConfigFileStringValue: open %s failed!\n", szWholePath);
? ? ? ? printf("GetConfigFileStringValue: open %s failed!\n", pszConfigFileName);
? ? ? ? return;
? ? }
? ? // 調用函數用于獲取具體配置項的值
?? ?GetStringContentValue(fp, pszSectionName, pszKeyName, pszOutput, iOutputLen);
? ? // 關閉文件
? ? fclose(fp);
? ? fp = NULL;
}
/**********************************************************************
* 功能描述: 從配置文件中獲取整型變量
* 輸入參數: pszSectionName-段名, 如: GENERAL
?? ??? ??? ?pszKeyName-配置項名, 如: EmployeeName
?? ??? ??? ?iDefaultVal-默認值
?? ??? ??? ?pszConfigFileName-配置文件名
* 輸出參數: 無
* 返 回 值: iGetValue-獲取到的整數值 ? -1-獲取失敗
* 其它說明: 無
* 修改日期 ? ? ? ? 版本號 ? ? ? 修改人 ? ? ? ?修改內容
* ------------------------------------------------------------------
* 20150507 ? ? ? ? V1.0 ? ? ? Zhao Yun ? ? ? ?創建
********************************************************************/
INT32 GetConfigFileIntValue(UINT8 *pszSectionName, UINT8 *pszKeyName, UINT32 iDefaultVal, UINT8 *pszConfigFileName)
{
? ? UINT8 ?szGetValue[512] = {0};
? ? INT32 ?iGetValue ? ? ? = 0;
? ? // 先對輸入參數進行異常判斷
?? ?if (pszSectionName == NULL || pszKeyName == NULL || pszConfigFileName == NULL)
? ? {
? ? ? ? printf("GetConfigFileIntValue: input parameter(s) is NULL!\n");
? ? ? ? return -1;
? ? }
? ? GetConfigFileStringValue(pszSectionName, pszKeyName, NULL, szGetValue, 512-1, pszConfigFileName); ? ?// 先將獲取的值存放在字符型緩存中
? ? if (szGetValue[0] == '\0' || szGetValue[0] == ';') ? ?// 如果是結束符或分號, 則使用默認值
? ? {
? ? ? ? iGetValue = iDefaultVal;
? ? }
? ? else
? ? {
? ? ? ? iGetValue = atoi(szGetValue);
? ? }
? ? return iGetValue;
}
*************************
總結
- 上一篇: 线性表:顺序队列算法实现
- 下一篇: C/C++编程操作Redis数据库,hi