我在VC下写 char szDrive[256]={0};
char szDir[256]={0};
char szPath[256]={0};
_splitpath(szFileName,szDrive,szDir,NULL,NULL);
GetPrivateProfileString("robot1", "a3", "1",str,256,filepath);
可以用,但在CE下就不行,没_splitpath和GetPrivateProfileString函数,怎么办那?
呵呵 自己写呀 后者我这里有现成的 给你帖出来
CString PGlobal::GetProfileString(const CString strSection, const CString strEntry,
const CString strDefault, DWORD nSize, const CString strIniPath)
{
if(strIniPath == L"" || strSection == L"" || strEntry == L"")
{
return strDefault;
}
CFile IniFile;
CString strCombine;
// TRY
{
// open target ini file.
if(! IniFile.Open(strIniPath, CFile::modeRead))
{
return strDefault;
}
// if the file is no data inside, return error.
if(IniFile.GetLength() == 0)
{
IniFile.Close();
return strDefault;
}
// read the string data for later use.
WCHAR *pBuf;
//the file is unicode, so data length must be twice size of that.
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(nSize); //create a big buffer.
strCombine = pBuf;
delete[] pBuf;
//see if the section, entry already exist.
int iIndex1, iIndex2, iIndex3, iIndexT;
iIndex1 = strCombine.Find(L"[" + strSection + L"]\r\n");
if(iIndex1 == -1) // not found section
{
IniFile.Close();
return strDefault;
}
iIndexT = iIndex1 + 4 + strSection.GetLength();
iIndex2 = strCombine.Find(strEntry + L"=", iIndexT);
if(iIndex2 == -1) // not found entry
{
IniFile.Close();
return strDefault;
}
else
{
//to find the entry key
iIndex3 = strCombine.Find(L"\r\n", iIndex2 + 1);
if(iIndex3 == -1)
{
IniFile.Close();
return strDefault;
}
// if find the entry key,
iIndexT = iIndex2 + 1 + strEntry.GetLength();
IniFile.Close();
//get the string between "enter key" and end of the string being searched before.
return strCombine.Mid(iIndexT, iIndex3 - iIndexT);
}
}
// CATCH(CFileException, e)
{
}
// END_CATCH //
IniFile.Close();
return strDefault;
}
只能自己写了啊,后者有现成的,帖给你,呵呵
CString PGlobal::GetProfileString(const CString strSection, const CString strEntry,
const CString strDefault, DWORD nSize, const CString strIniPath)
{
if(strIniPath == L"" || strSection == L"" || strEntry == L"")
{
return strDefault;
}
CFile IniFile;
CString strCombine;
// TRY
{
// open target ini file.
if(! IniFile.Open(strIniPath, CFile::modeRead))
{
return strDefault;
}
// if the file is no data inside, return error.
if(IniFile.GetLength() == 0)
{
IniFile.Close();
return strDefault;
}
// read the string data for later use.
WCHAR *pBuf;
//the file is unicode, so data length must be twice size of that.
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(nSize); //create a big buffer.
strCombine = pBuf;
delete[] pBuf;
//see if the section, entry already exist.
int iIndex1, iIndex2, iIndex3, iIndexT;
iIndex1 = strCombine.Find(L"[" + strSection + L"]\r\n");
if(iIndex1 == -1) // not found section
{
IniFile.Close();
return strDefault;
}
iIndexT = iIndex1 + 4 + strSection.GetLength();
iIndex2 = strCombine.Find(strEntry + L"=", iIndexT);
if(iIndex2 == -1) // not found entry
{
IniFile.Close();
return strDefault;
}
else
{
//to find the entry key
iIndex3 = strCombine.Find(L"\r\n", iIndex2 + 1);
if(iIndex3 == -1)
{
IniFile.Close();
return strDefault;
}
// if find the entry key,
iIndexT = iIndex2 + 1 + strEntry.GetLength();
IniFile.Close();
//get the string between "enter key" and end of the string being searched before.
return strCombine.Mid(iIndexT, iIndex3 - iIndexT);
}
}
// CATCH(CFileException, e)
{
}
// END_CATCH //
IniFile.Close();
return strDefault;
}
无语ing........ 这垃圾网络....... 帖多了 别拍我砖头.... @_@
举个例子好吗比方我想读取 CF卡上 config.ini 里的AAA=20的AAA 的值怎么写那?
[TEST]
AAA=20
第一个参数是Section, 就是[TEST]这个,第二个是入口点AAA,最后一个是文件名,成功则返回20,失败则返回"".
strValue = GetProfileString(_T("TEST"), _T("AAA"), _T(""), _MAX_PATH, L"config.ini ");