16,548
社区成员




#define MAX_SECTION 1000//section的最大字长度
wchar_t filetemp[MAX_PATH + 1];
::GetCurrentDirectory(MAX_PATH,filetemp);//取当前路径
wcscat(filetemp,L"\\setup.ini");//配置文件
wchar_t configStr[MAX_SECTION + 1];
DWORD sectionNum = GetPrivateProfileString(NULL,NULL,L"",configStr,MAX_SECTION,filetemp);//读取所有的section保存,section之间用NULL隔开
int posNum = 0;//标记遍历字符串时NULL的位置
wchar_t *temp = configStr + posNum;
for(posNum; posNum < sectionNum;)//configStr以两个NULL作为结束
{
m_funcNameList->AddString(temp);
posNum += wcslen(temp) + 1;
temp = configStr + posNum ;
}
代码不好,莫笑
#define MAX_BUFFER 1024 //the buffer for "String" or "SectionNames" return
CString CIniEntry::GetString(LPCTSTR lpIniFileName,LPCTSTR lpSectionName, LPCTSTR lpKeyName)
{
LPCTSTR lpDefault="";
char RetString[MAX_BUFFER]="\0";
LPTSTR lpRetString=RetString;
DWORD dwChar;
dwChar=GetPrivateProfileString(lpSectionName,lpKeyName,lpDefault,lpRetString,
sizeof(RetString),lpIniFileName);
if(!dwChar)
return "";
return (CString)lpRetString;
}
BOOL CIniEntry::WriteString(LPCTSTR lpIniFileName, LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpString)
{
ASSERT(lpIniFileName);
if(!lpSectionName)
return FALSE;
return(::WritePrivateProfileString(lpSectionName,lpKeyName,lpString,lpIniFileName));
}
CString CIniEntry::EnumSectionNames(LPCTSTR lpIniFileName,char chSpaceSign)
{
ASSERT(lpIniFileName);
if(!lpIniFileName)
return "";
DWORD dwSize;
char tsn[MAX_BUFFER]="\0";
LPTSTR lptsn=tsn;
CString strSn;
dwSize=::GetPrivateProfileSectionNames(lptsn,sizeof(tsn),lpIniFileName);
DWORD dwI=dwSize-2;
if(dwSize)
{
char sn[MAX_BUFFER]="\0";
strncpy(sn,&tsn[0],1);
for(DWORD i=1;i<dwI;i++)
{
if(tsn[i])
strncat(sn,&tsn[i],1);
else
strncat(sn,&chSpaceSign,1);
}
if(dwSize==MAX_BUFFER-2)
AfxMessageBox("Not All of the Sections Returned!",MB_ICONINFORMATION|MB_OK);
strSn=(CString) sn;
return strSn;
}
return "";
}
CString CIniEntry::GetSection(LPCTSTR lpIniFileName, LPCTSTR lpSectionName,char chSpaceSign)
{
ASSERT(lpIniFileName);
ASSERT(lpSectionName);
if(!lpSectionName||!lpIniFileName)
return "";
char tkn[MAX_BUFFER]="\0";
LPTSTR lptkn=tkn;
DWORD dwSize,dwI;
CString strSection;
dwSize=::GetPrivateProfileSection(lpSectionName,lptkn,sizeof(tkn),lpIniFileName);
dwI=dwSize-2;
if(dwSize)
{
char kn[MAX_BUFFER]="\0";
strncpy(kn,&tkn[0],1);
for(DWORD i=1;i<dwI;i++)
{
if(tkn[i])
strncat(kn,&tkn[i],1);
else
strncat(kn,&chSpaceSign,1);
}
if(dwSize==MAX_BUFFER-2)
AfxMessageBox("Not All of the Keys Returned!",MB_ICONINFORMATION|MB_OK);
strSection=(CString) kn;
return strSection;
}
return "";
}
CString CIniEntry::EnumKeys(LPCTSTR lpIniFileName, LPCTSTR lpSectionName,char chSpaceSign)
{
CString strKn;
strKn=GetSection(lpIniFileName,lpSectionName,chSpaceSign);
if(!strKn.IsEmpty())
{
CString kn;
int dwSize=strKn.GetLength();
// Find the first key of the section (before the first "=");
int nEqual=strKn.Find('='); // Index of the "=";
kn=strKn.Left(nEqual);
int nPos=nEqual+1;
//t1=12345,t2=67890,t3=fffff,t4=aaaaa,t5=asss
while((nEqual=strKn.Find('=',nPos))!=-1)
{
kn+=chSpaceSign;
int nSpaceSign=strKn.Find(chSpaceSign,nPos);
kn+=strKn.Mid(nSpaceSign+1,nEqual-nSpaceSign-1);
nPos=nEqual+1;
}
return kn;
}
return "";
}
#define MAX_BUFFER 1024 //the buffer for "String" or "SectionNames" return
CString CIniEntry::GetString(LPCTSTR lpIniFileName,LPCTSTR lpSectionName, LPCTSTR lpKeyName)
{
LPCTSTR lpDefault="";
char RetString[MAX_BUFFER]="\0";
LPTSTR lpRetString=RetString;
DWORD dwChar;
dwChar=GetPrivateProfileString(lpSectionName,lpKeyName,lpDefault,lpRetString,
sizeof(RetString),lpIniFileName);
if(!dwChar)
return "";
return (CString)lpRetString;
}
BOOL CIniEntry::WriteString(LPCTSTR lpIniFileName, LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpString)
{
ASSERT(lpIniFileName);
if(!lpSectionName)
return FALSE;
return(::WritePrivateProfileString(lpSectionName,lpKeyName,lpString,lpIniFileName));
}
CString CIniEntry::EnumSectionNames(LPCTSTR lpIniFileName,char chSpaceSign)
{
ASSERT(lpIniFileName);
if(!lpIniFileName)
return "";
DWORD dwSize;
char tsn[MAX_BUFFER]="\0";
LPTSTR lptsn=tsn;
CString strSn;
dwSize=::GetPrivateProfileSectionNames(lptsn,sizeof(tsn),lpIniFileName);
DWORD dwI=dwSize-2;
if(dwSize)
{
char sn[MAX_BUFFER]="\0";
strncpy(sn,&tsn[0],1);
for(DWORD i=1;i<dwI;i++)
{
if(tsn[i])
strncat(sn,&tsn[i],1);
else
strncat(sn,&chSpaceSign,1);
}
if(dwSize==MAX_BUFFER-2)
AfxMessageBox("Not All of the Sections Returned!",MB_ICONINFORMATION|MB_OK);
strSn=(CString) sn;
return strSn;
}
return "";
}
CString CIniEntry::GetSection(LPCTSTR lpIniFileName, LPCTSTR lpSectionName,char chSpaceSign)
{
ASSERT(lpIniFileName);
ASSERT(lpSectionName);
if(!lpSectionName||!lpIniFileName)
return "";
char tkn[MAX_BUFFER]="\0";
LPTSTR lptkn=tkn;
DWORD dwSize,dwI;
CString strSection;
dwSize=::GetPrivateProfileSection(lpSectionName,lptkn,sizeof(tkn),lpIniFileName);
dwI=dwSize-2;
if(dwSize)
{
char kn[MAX_BUFFER]="\0";
strncpy(kn,&tkn[0],1);
for(DWORD i=1;i<dwI;i++)
{
if(tkn[i])
strncat(kn,&tkn[i],1);
else
strncat(kn,&chSpaceSign,1);
}
if(dwSize==MAX_BUFFER-2)
AfxMessageBox("Not All of the Keys Returned!",MB_ICONINFORMATION|MB_OK);
strSection=(CString) kn;
return strSection;
}
return "";
}
CString CIniEntry::EnumKeys(LPCTSTR lpIniFileName, LPCTSTR lpSectionName,char chSpaceSign)
{
CString strKn;
strKn=GetSection(lpIniFileName,lpSectionName,chSpaceSign);
if(!strKn.IsEmpty())
{
CString kn;
int dwSize=strKn.GetLength();
// Find the first key of the section (before the first "=");
int nEqual=strKn.Find('='); // Index of the "=";
kn=strKn.Left(nEqual);
int nPos=nEqual+1;
//t1=12345,t2=67890,t3=fffff,t4=aaaaa,t5=asss
while((nEqual=strKn.Find('=',nPos))!=-1)
{
kn+=chSpaceSign;
int nSpaceSign=strKn.Find(chSpaceSign,nPos);
kn+=strKn.Mid(nSpaceSign+1,nEqual-nSpaceSign-1);
nPos=nEqual+1;
}
return kn;
}
return "";
}