EVC下如何读取配置文件? 如题,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://community.csdn.net/Expert/topic/5612/5612380.xml?temp=.9547541自己读写文件实现 #define MAX_LEN 1000BOOL WriteProfileString(const CString strSection, const CString strEntry, const CString strValue, const CString strIniPath){ if(strSection == L"" || strEntry == L"" || strValue == L"" || strIniPath == 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"; LPTSTR lpCombine = strCombine.GetBuffer(0); IniFile.Write(lpCombine, strCombine.GetLength() * 2); IniFile.Close(); return TRUE; } WCHAR *pBuf; pBuf = new WCHAR[IniFile.GetLength() / 2 + 1]; if(pBuf == NULL) { IniFile.Close(); return FALSE; } if(IniFile.Read(pBuf, IniFile.GetLength()) != IniFile.GetLength()) { delete[] pBuf; IniFile.Close(); return FALSE; } pBuf[IniFile.GetLength() / 2] = NULL; strCombine.GetBuffer(MAX_LEN); strCombine = pBuf; delete[] pBuf; 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"; 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"); 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); LPTSTR lpCombine = strCombine.GetBuffer(0); IniFile.SetLength(0); IniFile.SeekToBegin(); IniFile.Write(lpCombine, strCombine.GetLength() * 2); IniFile.Close(); return TRUE; } } CATCH(CFileException, e) { } END_CATCH IniFile.Close(); return FALSE;}CString GetProfileString(const CString strSection, const CString strEntry, const CString strDefault, const CString strIniPath){ if(strSection == L"" || strEntry == L"" || strIniPath == 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; 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, const CString strIniPath){ wchar_t cBuff[MAX_LEN]; CString strValue(""); _itow(iValue, cBuff, 10); strValue.Format(_T("%s"), cBuff); return WriteProfileString(strSection, strEntry, strValue, strIniPath);} int GetProfileInt(const CString strSection, const CString strEntry, const int iDefault, const CString strIniPath){ wchar_t cBuff[MAX_LEN]; CString strDefault(""); CString strReturn(""); _itow(iDefault, cBuff, 10); strDefault.Format(_T("%s"), cBuff); strReturn = GetProfileString(strSection, strEntry, strDefault, strIniPath); return (_wtoi(strReturn));} 编译通过了,我用了函数GetProfileString();里面的最后一个参数不知怎么赋值!我赋的是我的配置软件的绝对路径:如:_T("F:\\VC++学习\\lianxi\\configure1\\ini_test.ini",经调试是if(! IniFile.Open(strIniPath, CFile::modeRead)) { return strDefault; }这里没打开文件,请问如何解决? 使用标准库unique算法出错,得不到正确值 子类构造函数中调用父类带参的构造函数,而实际却调用父类默认构造函数,请问各位这是为何????? 急!vc++ socket怎么一次传数据库里查询的多条记录 有关结构:编译通过,为何执行时退出程序、死机? 高分求助, 怎样使用#pragma comment ( lib, "XXX.lib" ) VC安装问题,急!!! 关于windows DNA 和com的问题 CEditView如何改变字体颜色? 监视程序如何编写 为什么我接不到我的自定义消息???? 文档编辑器 一个顶级的,没有标题、菜单和边框的窗口,可见的,应该如何创建?
自己读写文件实现
{
if(strSection == L"" || strEntry == L"" || strValue == L"" || strIniPath == 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";
LPTSTR lpCombine = strCombine.GetBuffer(0);
IniFile.Write(lpCombine, strCombine.GetLength() * 2);
IniFile.Close();
return TRUE;
} WCHAR *pBuf;
pBuf = new WCHAR[IniFile.GetLength() / 2 + 1];
if(pBuf == NULL)
{
IniFile.Close();
return FALSE;
}
if(IniFile.Read(pBuf, IniFile.GetLength()) != IniFile.GetLength())
{
delete[] pBuf;
IniFile.Close();
return FALSE;
} pBuf[IniFile.GetLength() / 2] = NULL;
strCombine.GetBuffer(MAX_LEN);
strCombine = pBuf;
delete[] pBuf; 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"; 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"); 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); LPTSTR lpCombine = strCombine.GetBuffer(0);
IniFile.SetLength(0);
IniFile.SeekToBegin();
IniFile.Write(lpCombine, strCombine.GetLength() * 2);
IniFile.Close();
return TRUE;
}
}
CATCH(CFileException, e)
{
}
END_CATCH
IniFile.Close();
return FALSE;
}
CString GetProfileString(const CString strSection, const CString strEntry, const CString strDefault, const CString strIniPath)
{
if(strSection == L"" || strEntry == L"" || strIniPath == 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; 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, const CString strIniPath)
{
wchar_t cBuff[MAX_LEN];
CString strValue(""); _itow(iValue, cBuff, 10);
strValue.Format(_T("%s"), cBuff); return WriteProfileString(strSection, strEntry, strValue, strIniPath);
}
int GetProfileInt(const CString strSection, const CString strEntry, const int iDefault, const CString strIniPath)
{
wchar_t cBuff[MAX_LEN];
CString strDefault("");
CString strReturn(""); _itow(iDefault, cBuff, 10);
strDefault.Format(_T("%s"), cBuff); strReturn = GetProfileString(strSection, strEntry, strDefault, strIniPath); return (_wtoi(strReturn));
}
{
return strDefault;
}
这里没打开文件,请问如何解决?