/******************************************************************************* 函数名称: IniReadInt 功能说明: 无 输入参数: 无 输出参数: 无 返 回 值: >=0: 成功 < 0: 错误代码 说 明: 无 *******************************************************************************/ int IniReadInt( const char *v_pszFileName, const char *v_pszSection, const char *v_pszKey, int v_pszDefaultValue) { char szValue[INT_VALUE_LEN] = {0}; if (0 != IniReadString(v_pszFileName, v_pszSection, v_pszKey, szValue, INT_VALUE_LEN, NULL)) { return v_pszDefaultValue; } return atoi(szValue); }
int IniReadInteger (const char *section, const char *indent, int defaultresult, const char *inifilename) { int i, len; string str = IniReadString (section, indent, "NULL", inifilename); if (str.compare("NULL") == 0) { return defaultresult; } len = str.length(); if (len == 0) { return defaultresult; } for (i = 0; i < len; i++) { if ((str.at(i) < '0') || (str.at(i) > '9')) { return defaultresult; } } return atoi(str.c_str()); }
int main(int argc, char *argv[]) { const char szSection[] = "student"; const char szKeyName[] = "name"; const char szKeyAge[] = "age"; char szValue[TEST_BUF_SIZE]={0}; int age; if (0 != IniWriteString(TEST_INI_FILE, szSection, szKeyName, "Tony")) { printf("IniWriteString failed. [%s, %s, %s]\n", szSection, szKeyName, "Tony"); } else { printf("IniWriteString success. [%s, %s, %s]\n", szSection, szKeyName, "Tony"); } if (0 != IniWriteString(TEST_INI_FILE, szSection, szKeyAge, "20")) { printf("IniWriteString failed. [%s, %s, %s]\n", szSection, szKeyAge, "20"); } else { printf("IniWriteString Success. [%s, %s, %s]\n", szSection, szKeyAge, "20"); } if (0 != IniReadString(TEST_INI_FILE, szSection, szKeyName, szValue, TEST_BUF_SIZE, "")) { printf("IniReadString failed. [%s, %s]\n", szSection, szKeyName); } else { printf("IniReadString success. [%s, %s, %s]\n", szSection, szKeyName, szValue); } age = IniReadInt(TEST_INI_FILE, szSection, szKeyAge, 0); printf("IniReadInt here. [%s, %s, %d]\n", szSection, szKeyAge, age); /* 用例 */ if (0 != IniReadString(TEST_INI_FILE, "Main", "test case", szValue, TEST_BUF_SIZE, NULL)) { printf("IniReadString failed. [%s, %s]\n", "Main", "test case"); } else { printf("IniReadString success. [%s, %s, %s]\n", "Main", "test case", szValue); } /* 用例 */ if (0 != IniReadString(TEST_INI_FILE, "Main", "test case1", szValue, TEST_BUF_SIZE, NULL)) { printf("IniReadString failed. [%s, %s]\n", "Main", "test case1"); } else { printf("IniReadString success. [%s, %s, %s]\n", "Main", "test case1", szValue); } /* 用例 */ if (0 != IniWriteString(TEST_INI_FILE, "Main", "test case1", "TEST CASE1")) { printf("IniWriteString failed. [%s, %s]\n", "Main", "test case1"); } else { printf("IniWriteString success. [%s, %s]\n", "Main", "test case1"); } if (0 != IniReadString(TEST_INI_FILE, "Main", "test case1", szValue, TEST_BUF_SIZE, NULL)) { printf("IniReadString failed. [%s, %s]\n", "Main", "test case1"); } else { printf("IniReadString success. [%s, %s, %s]\n", "Main", "test case1", szValue); } return 0; }