1。 CStringList不完全用法。
CStringList是CString链表,在MFC编程中STL之外的另一选择,用起来更加简洁.
插入数据:AddTail();AddHead()
删除数据:RemoveAll();RemoveAt();RemoveHead();RemoveTail()
取得元素个数:GetCount()
取得某个元素值:GetAt(cstringList.FindIndex(index))//index为整数
取得头尾元素:GetHead() ;GetTail(),
遍历最好用上面的方式,不要用POSITION变量,用FindIndex可以了。
初始化:
CStringList listFileName;
listFileName.RemoveAll();
添加操作:
listFileName.AddTail(szFullPathName);
遍历操作:
POSITION rPos;
rPos = listFileName.GetHeadPosition();
while (rPos != NULL)
{
strFileName = listFileName.GetNext(rPos);
strLog += "/r/n" + strFileName;
}
2.
CStringList 总结
CStringList
└CStringList
|
构造一个空的CString对象列表 |
GetHead
| 返回此列表(不能是空的)中头部的元素 |
GetTail | 返回此列表(不能是空的)中尾部的元素 |
RemoveHead
| 从列表的头部删除元素 |
RemoveTail | 从列表的尾部删除元素 |
AddHead | 在列表的头部添加一个元素(或者是另一个列表中的所有元素),即产生一个新的头部 |
AddTail | 在列表的尾部添加一个元素(或者是另一个列表中的所有元素),即产生一个新的尾部 |
RemoveAll | 删除此列表中的所有元素 |
GetHeadPosition
| 返回列表中的头部元素的位置 |
GetTailPosition | 返回列表中尾部元素的位置 |
GetNext | 获取用于重复的下一个元素 |
GetPrev | 获取用于重复的前一个元素 |
获取给定位置处的元素 | |
SetAt | 设置给定位置处的元素 |
RemoveAt | 从此列表中删除由位置指定的一个元素 |
InsertBefore
| 在一个给定位置之前插入一个新元素 |
InsertAfter | 在一个给定位置之后插入一个新元素 |
获取由字符串值指定的元素的位置 | |
FindIndex | 获取由一个从零开始的索引指定的元素的位置 |
返回此列表中的元素个数 | |
IsEmpty | 测试列表是否为空(没有元素) |
3.
①建立一个控制台工程,支持MFC,命名为TestINI;
②在TestINI.cpp中添加头文件Ini.h
③在主函数中添加测试代码,如下:
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
// INI文件的全路径名
CString m_strFileName;
// 获取当前模块的路径
GetModuleFileName( AfxGetInstanceHandle(), m_strFileName.GetBuffer( MAX_PATH ), MAX_PATH );
m_strFileName.ReleaseBuffer( MAX_PATH );
// 将获得的路径替换为设置的INI文件路径
m_strFileName.Replace( _T( "TestINI.exe" ), _T( "1.ini" ) );
// INI文件
CIni iniFile;
// 设置INI文件全路径名
iniFile.setINIFileName(m_strFileName);
// 设置键值
iniFile.setKey("TRUE","AUTORUN","SYSTEM");
iniFile.setKey("","","HELLO");
iniFile.setKey("","WORLD","HELLO");
iniFile.setKey("TRUE","WORLD","HELLO");
iniFile.setKey("C:\\1.MP3","MP3","MUSIC");
iniFile.setKey("FALSE","AUTO","LOCK");
// 获得键值
::AfxMessageBox(iniFile.getKeyValue("AUTORUN","SYSTEM"));
::AfxMessageBox(iniFile.getKeyValue("MP3","MUSIC"));
}
return nRetCode;
}
附录:
- Ini.h
- Ini.cpp
// Ini.h
#include <afxcoll.h>
class CIni
{
public:
// 设定INI文件的名字,要完全路径名
void setINIFileName(CString strINIFile);
// 获取段类所有数据
CStringList* getSectionData(CString strSection);
// 所有段名
CStringList* getSectionNames();
// 判断段名是否已经存在
BOOL sectionExists(CString strSection);
// 设置键值
long setKey(CString strValue, CString strKey, CString strSection);
// 获得键值
CString getKeyValue(CString strKey,CString strSection);
// 构造函数
CIni()
{
m_sectionList = new CStringList();
m_sectionDataList = new CStringList();
}
// 带文件名参数的构造函数
CIni(CString strFile)
{
m_strFileName = strFile;
m_sectionList = new CStringList();
m_sectionDataList = new CStringList();
}
// 析构清空链表释放
~CIni()
{
delete m_sectionList;
delete m_sectionDataList;
}
private:
CStringList *m_sectionDataList;
CStringList *m_sectionList;
CString m_strSection;
long m_lRetValue;
CString m_strFileName;
};
// Ini.cpp
#include "stdafx.h"
#include "INI.h"
#include <afxcoll.h>
CString CIni::getKeyValue(CString strKey,CString strSection)
{
char ac_Result[255];
m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,
"",ac_Result, 255, (LPCTSTR)m_strFileName);
CString strResult(ac_Result);
return strResult;
}
long CIni::setKey(CString strValue, CString strKey, CString strSection)
{
m_lRetValue = WritePrivateProfileString (strSection, strKey,
strValue, m_strFileName);
return m_lRetValue;
}
BOOL CIni::sectionExists(CString strSection)
{
char ac_Result[100];
CString csAux;
m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL,
"",ac_Result, 90, (LPCTSTR)m_strFileName);
return (m_lRetValue > 0);
}
CStringList* CIni::getSectionNames()
{
char ac_Result[2000];
m_sectionList->RemoveAll();
m_lRetValue = GetPrivateProfileSectionNames(ac_Result,2000,(LPCTSTR)m_strFileName);
CString strSectionName;
for(int i=0; i<m_lRetValue; i++)
{
if(ac_Result[i] != '\0') {
strSectionName = strSectionName + ac_Result[i];
} else {
if(strSectionName != "") {
m_sectionList->InsertAfter(m_sectionList->GetTailPosition(),strSectionName);
}
strSectionName = "";
}
}
return m_sectionList;
}
CStringList* CIni::getSectionData(CString strSection)
{
char ac_Result[2000];
m_sectionDataList->RemoveAll();
m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, 2000, (LPCTSTR)m_strFileName);
CString strSectionData;
for(int i=0; i<m_lRetValue; i++)
{
if(ac_Result[i] != '\0') {
strSectionData = strSectionData + ac_Result[i];
} else {
if(strSectionData != "") {
m_sectionDataList->InsertAfter(m_sectionDataList->GetTailPosition(),strSectionData);
}
strSectionData = "";
}
}
return m_sectionDataList;
}
void CIni::setINIFileName(CString strINIFile)
{
m_strFileName = strINIFile;
}
//