{A}简介
这是一个进一步的探索,如果INI文件类主题象雄健(xiaohe521)和亚当(aejw)开始的代码项目。其主要思想仍然是几乎相同的:类是一个读/写INI文件的Win32 API周围的瘦包装。但是,我试图齿轮对MFC我的课。
这个类可能是有用的,如果你的应用程序需要访问注册表和INI文件,或多个INI文件。否则,你可以使用AfxGetApp()- GT; m_pszProfileName和AfxGetApp()- GT GetProfileXXXX (???.类的声明
{C}class CIniFile : public CObject
{
public:
CIniFile(LPCTSTR lpIniFileName, INT iMaxStringLength);
virtual CIniFile();
// Operations
public:
CString GetIniFileName();
void SetIniFileName(LPCTSTR lpIniFileName);
BOOL GetStatus(CFileStatus& rStatus);
void GetString(LPCTSTR lpSection, LPCTSTR lpKey,
CString& strRet, LPCTSTR strDefault);
UINT GetInt(LPCTSTR lpSection, LPCTSTR lpKey, INT iDefaultValue);
FLOAT GetFloat(LPCTSTR lpSection, LPCTSTR lpKey, FLOAT fDefaultValue);
BOOL GetStruct(LPCTSTR lpSection, LPCTSTR lpKey,
LPVOID lpRetStruct, UINT iSizeStruct);
void GetSectionNames(CStringList& lstSectionNames);
BOOL WriteSection(LPCTSTR lpSection, LPCTSTR lpData);
BOOL WriteString(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpString);
BOOL WriteNumber(LPCTSTR lpSection, LPCTSTR lpKey, INT iValue);
BOOL WriteNumber(LPCTSTR lpSection, LPCTSTR lpKey, FLOAT fValue);
BOOL WriteStruct(LPCTSTR lpSection, LPCTSTR lpKey,
LPVOID lpStruct, UINT iSizeStruct);
类实现 BOOL RemoveKey(LPCTSTR lpSection, LPCTSTR lpKey);
};
的实现非常简单。它是基于以下的API函数:GetPrivateProfileSectionNamesGetPrivateProfileStructGetPrivateProfileStringGetPrivateProfileIntWritePrivateProfileStructWritePrivateProfileStringWritePrivateProfileSection
下载源代码的细节(这是相当良好的注释)。演示应用/测试台
下面的控制台应用程序,它的类的方法。输出去的afxDump。// just a dummy structure to write/read to/from the INI file
struct TestStruct : public POINT
{
char charr[8];
};
// PURPOSE: test bed for the INI file class
void TestIniClass()
{
CIniFile iniFile(".\\test1.ini", 1024);
iniFile.SetIniFileName(".\\test2.ini"); // change the file name
// after the class has bee created
// test the writing to the INI file
iniFile.WriteSection("section1", "key1=test1\x000key2=test2"); // make a section
// with 2 new keys
iniFile.WriteSection("section2", "key1=test1"); // make another section
iniFile.WriteString("section1", "key3", "test3"); // create a new key
iniFile.WriteString("section1", "key2", "test4"); // update an existing key
iniFile.WriteString("section1", "key1", NULL); // remove an existing key
iniFile.RemoveKey("section1", "key3"); // remove another existing key
iniFile.WriteNumber("section1", "key4", 123); // write an integer
// to a new key
iniFile.WriteNumber("section1", "key5", -123); // write a negative integer
// to a new key
iniFile.WriteNumber("section1", "key6", -123.456f); // write a float to a new key
TestStruct writeTestStruct;
writeTestStruct.x = 6;
writeTestStruct.y = 7;
strcpy(writeTestStruct.charr, "abcdefg");
iniFile.WriteStruct("section1", "key7", &writeTestStruct, sizeof(TestStruct));
// Test the reading from the INI file
CString str; // string that will receive the output
iniFile.GetString("section1", "key2", str, "default"); TRACE("key2=%s\n", str);
iniFile.GetString("section1", "nokey", str, "default");
TRACE("nokey=%s\n", str); // non-existent key
iniFile.GetString("section3", "key1", str, "default"); TRACE("nokey=%s\n", str);
TRACE("key4=%d\n", iniFile.GetInt("section1", "key4", 0));
TRACE("key5=%d\n", iniFile.GetInt("section1", "key5", 0));
TRACE("key6=%f\n", iniFile.GetFloat("section1", "key6", 0));
TestStruct readTestStruct;
iniFile.GetStruct("section1", "key7", &readTestStruct, sizeof(TestStruct));
TRACE("key7= %ld %ld %s\n", readTestStruct.x,
readTestStruct.y, readTestStruct.charr);
// Test the section list retrieval
CStringList lstSectionNames;
iniFile.GetSectionNames(lstSectionNames);
TRACE("Sections:\n");
for (POSITION pos = lstSectionNames.GetHeadPosition(); pos != NULL; )
{
TRACE("\t%s\n", lstSectionNames.GetNext(pos));
}
}
有什么应该是固定的,补充或在这个项目中中删除的吗?请让我知道!参考文献
[1]
[2]
[3] 修订历史冯0.1初稿尼克Alexeev2006年11月24日,日冯0.2新增的异常处理尼克Alexeev2006年11月30日,日