VC ini文件另类用法
Ini文件一般用来做配置文件,前几天在做一个可以保存本地用户最近查过单词的功能。例如,有一个combobox,将这些单词按时间顺序显示出来,并且如果遇到相同单词的话,将旧的删了,新查的显示在前面。由于最近都在做软件的基本设置,对ini产生了感情,呵呵,就试着用ini实现以上的功能。虽然有点复杂,但还是把代码贴一下。
void CGWEDICTDlg::CheckedWord(CString str)
{
int sum;
int i;
sum =::GetPrivateProfileInt("SUM","sum",0,".//checkedword.ini");
CString str1;
CString str2;
CString str3;
LPCTSTR lp;
LPCTSTR lp1;
LPCTSTR lp2;
int j=0;
int k=0;
int l=0;
CString checkedword="";
str.MakeLower();
/保存20个最近查过单词/
if(sum==20)
{
for(i=0;i<sum;i++)
{
str1.Format("%d",i);
lp = str1;
if(::GetPrivateProfileString("CheckedWord",lp,NULL,checkedword.GetBuffer(MAX_PATH),MAX_PATH,".//checkedword.ini"))
{
checkedword.MakeLower();
查找新查的单词是否在最近保存记录中/
if(checkedword==str)
{
k=1; //用来做判断
}
}
}
if(k==1)
{
for(i=0;i<sum;i++)
{
str1.Format("%d",i);
lp = str1;
str3.Format("%d",j);
lp2=str3;
if(::GetPrivateProfileString("CheckedWord",lp,NULL,checkedword.GetBuffer(MAX_PATH),MAX_PATH,".//checkedword.ini"))
{
checkedword.MakeLower();
if(checkedword!=str)
{
//重新写ini文件,有相同单词的就不写到原来的位置,写到最新的位置 ::WritePrivateProfileString("CheckedWord",lp2,checkedword, ".//checkedword.ini");
l=0;
}
else
{
if(i!=0)
{
j=j-1;
}
else
{
l=1;
}
}
}
if(l!=1)
{
j++;
}
}
str1.Format("%d",j);
lp = str1;
m_checkword.InsertString(0,str);
::WritePrivateProfileString("CheckedWord",lp,str, ".//checkedword.ini");
}
/新查单词没有在记录中//
else
{
for(i=0;i<sum;i++)
{
if(i!=0)
{
str1.Format("%d",i);
lp = str1;
str3.Format("%d",j);
lp2=str3;
if(::GetPrivateProfileString("CheckedWord",lp,NULL,checkedword.GetBuffer(MAX_PATH),MAX_PATH,".//checkedword.ini"))
{
checkedword.MakeLower();
if(checkedword!=str)
{
::WritePrivateProfileString("CheckedWord",lp2,checkedword, ".//checkedword.ini");
l=0;
}
}
}
else
{
l=1;
}
if(l!=1)
{
j++;
}
}
str1.Format("%d",j);
lp = str1;
m_checkword.InsertString(0,str);
::WritePrivateProfileString("CheckedWord",lp,str, ".//checkedword.ini");
}
}
//ini文件没有保存够20个单词的情况//
if(sum!=20)
{
for(i=0;i<sum;i++)
{
str1.Format("%d",i);
lp = str1;
str3.Format("%d",j);
lp2=str3;
if(::GetPrivateProfileString("CheckedWord",lp,NULL,checkedword.GetBuffer(MAX_PATH),MAX_PATH,".//checkedword.ini"))
{
checkedword.MakeLower();
if(checkedword!=str)
{
::WritePrivateProfileString("CheckedWord",lp2,checkedword, ".//checkedword.ini");
l=0;
}
else
{ if(i!=0)
{
j=j-1;
}
else
{
l=1;
}
}
}
if(k!=1)
{
j++;
}
}
str1.Format("%d",j);
lp = str1;
m_checkword.InsertString(0,str);
::WritePrivateProfileString("CheckedWord",lp,str, ".//checkedword.ini");
if(j==i)
{
sum=sum+1;
}
str2.Format("%d",sum);
lp1=str2;
::WritePrivateProfileString("SUM","sum",lp1,".//checkedword.ini");
}
m_checkword.ResetContent();
SetDlgItemText(IDC_COMBO1,str);
int sum1;
sum1 =::GetPrivateProfileInt("SUM","sum",0,".//checkedword.ini");
checkedword="";
for(i=0;i<sum1;i++)
{
str1.Format("%d",i);
lp = str1;
if(::GetPrivateProfileString("CheckedWord",lp,NULL,checkedword.GetBuffer(MAX_PATH),MAX_PATH,".//checkedword.ini"))
{
m_checkword.InsertString(0,checkedword);
}
}
}
我的ini文件格式:
[CheckedWord]
0=abhor
1=abase
2=adolescence
3=apple
4=abate
5=ablaze
6=abash
7=abet
8=abhorrent
9=abide
10=abolish
11=aborigines
12=abuse
13=abrupt
14=abbreviate
15=abrupt
16=abominate
17=absolve
18=accede
19=abandon
[SUM]
sum=20