?? code.cpp
字號:
//////////////////////////////////////////////////////////////////////////////////////
///// 說 明
////
// 1. 如果下列函數執行時返回錯誤,用GetLastError()得到錯誤信息
// 2. 讀和寫都按標準的Ini文件格式
// 3. 在CATCH{}宏里可以得到異常信息
#define MAX_LEN 1000 ///用于CString,使用CString類前最好估算整個操作過程中可能出現的最大
///字符長度,用GetBuffer(MAX_LEN)來創建緩沖區。這將避免頻繁創建、刪除
///緩沖區,提高了效率,也避免了內存碎片。
CString strIniPath = L"\\hard disk\\user.ini"; ///全局變量,預先設置一個文件路徑。
///文件路徑沒有盤符,如C:\ 、D:\ 。
///必須為絕對路徑,CE下沒相對路徑概念。
BOOL WriteProfileString(const CString strSection, const CString strEntry, const CString strValue)
{
////先判斷所有參數是否為空
if(strSection == L"" || strEntry == L"" || strValue == L"")
{
return FALSE;
}
CFile IniFile;
CString strCombine;
TRY
{
////打開文件
if(! IniFile.Open(strIniPath, CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate))
{
return FALSE;
}
///判斷文件是否為空,為空則直接寫數據,就不必先把文件內容讀出來。
if(IniFile.GetLength() == 0)
{
strCombine = L"[" + strSection + L"]" + L"\r\n"
+ strEntry + L"=" + strValue + L"\r\n";
///得到strCombine包含的緩沖區首地址(方法有兩種)
LPTSTR lpCombine = strCombine.GetBuffer(0);
IniFile.Write(lpCombine, strCombine.GetLength() * 2);
IniFile.Close();
return TRUE;
}
///讀出文件所有數據到緩沖區
WCHAR *pBuf;
pBuf = new WCHAR[IniFile.GetLength() / 2 + 1]; ///文件內容為UNICODE,所以文件長度一定是2的倍數
if(pBuf == NULL)
{
IniFile.Close();
return FALSE;
}
if(IniFile.Read(pBuf, IniFile.GetLength()) != IniFile.GetLength())
{
delete[] pBuf;
IniFile.Close();
return FALSE;
}
///把緩沖區地址賦給CString對象,為了使用CString包含的函數。
///一般Ini文件比較小,所以從緩沖區到CString的復制過程用時很短、耗費資源也很少
pBuf[IniFile.GetLength() / 2] = NULL;
strCombine.GetBuffer(MAX_LEN); ///先創建大的緩沖區
strCombine = pBuf;
delete[] pBuf;
//////開始查找,看section和entry是否已經存在
int iIndex1, iIndex2, iIndex3, iIndexT;
iIndex1 = strCombine.Find(L"[" + strSection + L"]\r\n");
if(iIndex1 == -1) ///沒找到
{
strCombine += L"[" + strSection + L"]" + L"\r\n"
+ strEntry + L"=" + strValue + L"\r\n";
///得到strCombine包含的緩沖區首地址
LPTSTR lpCombine = strCombine.GetBuffer(0);
IniFile.SetLength(0); ///刪除原來數據
IniFile.SeekToBegin();
IniFile.Write(lpCombine, strCombine.GetLength() * 2);
IniFile.Close();
return TRUE;
}
iIndexT = iIndex1 + 4 + strSection.GetLength();
iIndex2 = strCombine.Find(strEntry + L"=", iIndexT);
if(iIndex2 == -1) ///沒找到
{
strCombine.Insert(iIndexT, strEntry + L"=" + strValue + L"\r\n");
///得到strCombine包含的緩沖區首地址
LPTSTR lpCombine = strCombine.GetBuffer(0);
IniFile.SetLength(0);
IniFile.SeekToBegin();
IniFile.Write(lpCombine, strCombine.GetLength() * 2);
IniFile.Close();
return TRUE;
}
else
{
iIndex3 = strCombine.Find(L"\r\n", iIndex2 + 1);
if(iIndex3 == -1)
{
IniFile.Close();
return FALSE;
}
iIndexT = iIndex2 + 1 + strEntry.GetLength();
strCombine.Delete(iIndexT, iIndex3 - iIndexT);
strCombine.Insert(iIndexT, strValue);
///得到strCombine包含的緩沖區首地址
LPTSTR lpCombine = strCombine.GetBuffer(0);
IniFile.SetLength(0);
IniFile.SeekToBegin();
IniFile.Write(lpCombine, strCombine.GetLength() * 2);
IniFile.Close();
return TRUE;
}
} ///end TRY
CATCH(CFileException, e)
{
/* ////用于調試,得到出錯信息
CString strT;
switch(e->m_cause)
{
case CFileException::generic:
case CFileException::badPath:
case CFileException::accessDenied:
case CFileException::hardIO:
case CFileException::diskFull:
...............
default:
}
*/ }
END_CATCH ///結束調試宏
IniFile.Close();
return FALSE;
}
CString GetProfileString(const CString strSection, const CString strEntry, const CString strDefault)
{
////先判斷前兩個參數是否為空
if(strSection == L"" || strEntry == L"")
{
return strDefault; ///不成功則返回默認值
}
CFile IniFile;
CString strCombine;
TRY
{
////打開文件
if(! IniFile.Open(strIniPath, CFile::modeRead))
{
return strDefault;
}
///判斷文件是否為空
if(IniFile.GetLength() == 0)
{
IniFile.Close();
return strDefault;
}
///讀出文件所有數據到緩沖區
WCHAR *pBuf;
pBuf = new WCHAR[IniFile.GetLength() / 2 + 1];
if(pBuf == NULL)
{
IniFile.Close();
return strDefault;
}
if(IniFile.Read(pBuf, IniFile.GetLength()) != IniFile.GetLength())
{
delete[] pBuf;
IniFile.Close();
return strDefault;
}
pBuf[IniFile.GetLength() / 2] = NULL;
strCombine.GetBuffer(MAX_LEN); ///先創建大的緩沖區
strCombine = pBuf;
delete[] pBuf;
//////開始查找,看section和entry是否已經存在
int iIndex1, iIndex2, iIndex3, iIndexT;
iIndex1 = strCombine.Find(L"[" + strSection + L"]\r\n");
if(iIndex1 == -1) ///沒找到
{
IniFile.Close();
return strDefault;
}
iIndexT = iIndex1 + 4 + strSection.GetLength();
iIndex2 = strCombine.Find(strEntry + L"=", iIndexT);
if(iIndex2 == -1) ///沒找到
{
IniFile.Close();
return strDefault;
}
else
{
iIndex3 = strCombine.Find(L"\r\n", iIndex2 + 1);
if(iIndex3 == -1)
{
IniFile.Close();
return strDefault;
}
iIndexT = iIndex2 + 1 + strEntry.GetLength();
IniFile.Close();
return strCombine.Mid(iIndexT, iIndex3 - iIndexT);
}
}
CATCH(CFileException, e)
{
}
END_CATCH ///結束調試宏
IniFile.Close();
return strDefault;
}
BOOL WriteProfileInt(const CString strSection, const CString strEntry, const int iValue)
{
////代碼同WriteProfileString相似。
////只需將int iValue轉成CString strValue
////使用函數:wchar_t * _itow( int value, wchar_t *string, int radix );
return TRUE;
}
int GetProfileInt(const CString strSection, const CString strEntry, const int iDefault)
{
////代碼同GetProfileString相似。
////只需將得到的CString類型轉換成int型
////使用函數:int _wtoi( const wchar_t *string );
return iDefault;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -