VC++ 操作 ini 文件
{
return m_strFileFullName;
}
BOOL CIniFile::IniFile_ModifyInt(LPCTSTR lpSectionName,
LPCTSTR lpKeyName,
int nValue)
{
return IniFile_WriteInt(lpSectionName,lpKeyName,nValue);
}
BOOL CIniFile::IniFile_ModifyString(LPCTSTR lpSectionName,
LPCTSTR lpKeyName,
LPCTSTR lpString)
{
return IniFile_WriteString(lpSectionName, lpKeyName, lpString);
}
BOOL CIniFile::IniFile_WriteLongString(LPCTSTR lpValue,
LPCTSTR lpSectionName,
LPCTSTR lpKeyName,
...)
{
// 此函数不允许键名和键值为空的
va_list pArgList;
LPCTSTR p;
if (lpValue == NULL ||
lpSectionName == NULL ||
lpKeyName == NULL)
return FALSE;
CString strSectionName(lpSectionName);
CString strKeyName(lpKeyName);
BOOL result = FALSE;
va_start(pArgList,lpKeyName); /* Initialize variable arguments. */
do {
p = va_arg(pArgList,LPCTSTR);
CString strTempValue1(p);
if (strTempValue1 == "ListEnd" ||
strTempValue1 == _T(""))
break;
if (!IniFile_WriteString(strSectionName,strKeyName,strTempValue1))
return FALSE;
p = va_arg(pArgList,LPCTSTR);
CString strTempValue2(p);
if (strTempValue2 == "ListEnd" ||
strTempValue2 == _T(""))
return FALSE;
strSectionName = strTempValue1;
strKeyName = strTempValue2;
} while(1);
va_end( pArgList ); /* Reset variable arguments. */
if (!IniFile_WriteString(strSectionName,strKeyName,lpValue))
return FALSE;
return TRUE;
}
BOOL CIniFile::IniFile_DeleteString(CString strSectionName, CString strKeyName)
{
return WritePrivateProfileString(strSectionName,strKeyName,NULL,m_strFileFullName);
}
BOOL CIniFile::IniFile_DeleteSection(CString strSectionName)
{
return WritePrivateProfileString(strSectionName,NULL,NULL,m_strFileFullName);
// return WritePrivateProfileSection(strSectionName,NULL,m_strFileFullName);
//这种方法会使 section 的标题一并删除
// return WritePrivateProfileSection(strSectionName,"",m_strFileFullName);
//这种方法会保留 section 的标题
}
BOOL CIniFile::IniFile_WriteSection(LPCTSTR lpSectionName, LPCTSTR lpString)
{
return WritePrivateProfileSection(lpSectionName,lpString,m_strFileFullName);
}
BOOL CIniFile::IniFile_WriteStruct(LPCTSTR lpSectionName,
LPCTSTR lpszKey,
LPVOID lpStruct,
UINT uSizeStruct)
{
return WritePrivateProfileStruct(lpSectionName,
lpszKey,
lpStruct,
uSizeStruct,
m_strFileFullName);
}
BOOL CIniFile::IniFile_DeleteStruct(LPCTSTR lpSectionName,
LPCTSTR lpszKey)
{
return WritePrivateProfileStruct(lpSectionName,lpszKey,NULL,1,m_strFileFullName);
}
BOOL CIniFile::SetFileName(LPCTSTR lpFileName)
{
if (!lpFileName)
return FALSE;
CString strFileName(lpFileName);
CString strPathName;
strFileName.TrimLeft();
strFileName.TrimRight();
if (strFileName.Find('\\') == -1)
{
TCHAR exeFullPath[MAX_PATH];
int len2 = GetModuleFileName(NULL,exeFullPath,MAX_PATH);
CString strDir(exeFullPath);
strDir.TrimLeft();
strDir.TrimRight();
int index = strDir.ReverseFind('\\');
strPathName = strDir.Left(index + 1) + strFileName;
}
else
strPathName = strFileName;
CString str;
if (strPathName.GetLength() > 4)
{
str = strPathName.Right(4);
str.MakeLower();
if (str == _T(".ini"))
{
m_strFileFullName = strPathName;
return TRUE;
}
}
strPathName += _T(".ini");
m_strFileFullName = strPathName;
return TRUE;
}
void CIniFile::IniFile_InitializeForCreate()
{
IniFile_WriteString("administrator","sa","sa\r\n");
IniFile_WriteString("user","abc","abc");
IniFile_WriteString("user","ab","ab\r\n");
}
DWORD CIniFile::IniFile_GetSectionNames(LPTSTR lpszReturnBuffer, DWORD nSize)
{
return GetPrivateProfileSectionNames(lpszReturnBuffer,nSize,m_strFileFullName);
}
DWORD CIniFile::IniFile_GetSectionNames(CStringArray &strArray)
{
char *sz = new char[nMaxSize_All_SectionNames];
DWORD dw = IniFile_GetSectionNames(sz,nMaxSize_All_SectionNames);
strArray.RemoveAll();
char * index = sz;
do {
CString str(index);
if (str.GetLength() < 1)
{
delete []sz;
return dw;
}
strArray.Add(str);
index = index + str.GetLength() + 1;
} while(index && (index < sz + nMaxSize_All_SectionNames));
delete []sz;
return dw;
}
BOOL CIniFile::IniFile_GetStruct(LPCTSTR lpSectionName,
LPCTSTR lpszKey,
LPVOID lpStruct,
UINT uSizeStruct)
{
return GetPrivateProfileStruct(lpSectionName,
lpszKey,
lpStruct,
uSizeStruct,
m_strFileFullName);
}
DWORD CIniFile::IniFile_GetSection(LPCTSTR lpSectionName,
LPTSTR lpReturnedString,
DWORD nSize)
{
return GetPrivateProfileSection(lpSectionName,
lpReturnedString,
nSize,
m_strFileFullName);
}
DWORD CIniFile::IniFile_GetKeyNamesValues(CString strSectionName, CStringArray &strArray)
{
char *sz = new char[nMaxSize_A_Section];
DWORD dw = IniFile_GetSection(strSectionName,sz,nMaxSize_A_Section);
char * index = sz;
CString strName,strValue;
int nPosition = -1;
while (index && (index < sz + dw))
{
CString str(index);
if (str.GetLength() < 1)
{
delete []sz;
return dw;
}
if ((nPosition = str.Find(_T('='))) == -1)
{
IniFile_DeleteString(strSectionName,str);
// continue;
}
else
{
strName = str.Left(nPosition);
strValue = str.Mid(nPosition + 1);
/*
if (strValue.GetLength() < 1)
{
IniFile_DeleteString(strSectionName,strName);
// continue;
}
*/
strArray.Add(strName);
strArray.Add(strValue);
}
index = index + str.GetLength() + 1;
}
delete []sz;
return dw;
}
void CIniFile::SetMaxSize_A_Section(UINT nSize)
{
nMaxSize_A_Section = nSize;
}
void CIniFile::SetMaxSize_All_SectionNames(UINT nSize)
{
nMaxSize_All_SectionNames =
Tags:VC,++,操作,ini,文件

