如何从ini文件里获取一个section,取得里面所有的值,初始化list control

时间:2022-03-24 04:29:06
m_cbCourse.AddString(L"全部课程");
m_cbCourse.SetCurSel(0);//让其默认显示第一项

m_lcShow.InsertColumn(1, L"学科");
m_lcShow.InsertColumn(0, L"姓名");
m_lcShow.InsertColumn(2, L"成绩");
m_lcShow.SetColumnWidth(1, 80);
m_lcShow.SetColumnWidth(0, 80);
m_lcShow.SetColumnWidth(2, 60);

CString tempStr1, tempStr2, strKey, strValue;
//tempStr1,tempStr2,strKey, strValue分别存放SectionName,键值对,键名,值
TCHAR SectionNames[MAX_PATH],Section[MAX_PATH];
ZeroMemory(SectionNames, MAX_PATH);
ZeroMemory(Section, MAX_PATH);

GetPrivateProfileSectionNames(SectionNames, MAX_PATH, L".\\mySyllabus.ini");

for (int i = 0; i < MAX_PATH - 2; i++)
{
if (0 == i && SectionNames[i] != '\0')
{
tempStr1 = SectionNames;
m_cbCourse.AddString(tempStr1);
//i += tempStr1.GetLength();
}
else if (SectionNames[i] == '\0')
{
if (SectionNames[i+1] == '\0')
break;//两个\0表示结束

tempStr1 = SectionNames + i +1;
m_cbCourse.AddString(tempStr1);
//i += tempStr1.GetLength();
}
GetPrivateProfileSection(tempStr1, Section, MAX_PATH, L".\\mySyllabus.ini");
for (int j = 0; j < MAX_PATH - 2; j++)
{
if (0 == j && SectionNames[j] != '\0')
{
tempStr2 = Section;
//m_cbCourse.AddString(tempStrKey);
int npos = tempStr2.Find('=');
strKey = tempStr2.Left(npos);
strValue = tempStr2.Mid(npos + 1);

int nItemRow = m_lcShow.GetItemCount();
int nIndex =m_lcShow.InsertItem(nItemRow, strKey);
m_lcShow.SetItemText(nIndex, 1, tempStr1);
m_lcShow.SetItemText(nIndex, 2, strValue);
}
else if (SectionNames[j] == '\0')
{
if (SectionNames[j+1] == '\0')
break;//两个\0表示结束

tempStr2 = Section + i +1;
int npos = tempStr2.Find('=');
strKey = tempStr2.Left(npos);
strValue = tempStr2.Mid(npos + 1);

int nItemRow = m_lcShow.GetItemCount();
int nIndex =m_lcShow.InsertItem(nItemRow, strKey);
m_lcShow.SetItemText(nIndex, 1, tempStr1);
m_lcShow.SetItemText(nIndex, 2, strValue);
}
}
}

我的代码在上面,结果要都像第一行那样,列出所有成绩 如何从ini文件里获取一个section,取得里面所有的值,初始化list control

5 个解决方案

#1


遇到的问题是什么?

#2


int nItemRow = m_lcShow.GetItemCount();
                m_lcShow.InsertItem(nItemRow, _T(""));
               m_lcShow.SetItemText(nItemRow , 0, strKey);
                m_lcShow.SetItemText(nItemRow , 1, tempStr1);
                m_lcShow.SetItemText(nItemRow , 2, strValue);

#3


昨天回去自己解决了,可能有不到位的地方,麻烦大家指正
CString tempStr1, tempStr2, strKey, strValue;
//tempStr1,tempStr2,strKey, strValue分别存放SectionName,键值对,键名,值
TCHAR SectionNames[MAX_PATH],Section[MAX_PATH];
ZeroMemory(SectionNames, MAX_PATH);
ZeroMemory(Section, MAX_PATH);

GetPrivateProfileSectionNames(SectionNames, MAX_PATH, L".\\mySyllabus.ini");

for (int i = 0; i < MAX_PATH - 2; i++)
{
if (0 == i && SectionNames[i] != '\0')
{
tempStr1 = SectionNames;
m_cbCourse.AddString(tempStr1);
GetPrivateProfileSection(tempStr1, Section, MAX_PATH, L".\\mySyllabus.ini");
for (int j = 0; j < MAX_PATH - 2; j++)
{
if (0 == j && Section[j] != '\0')
{
tempStr2 = Section;
//m_cbCourse.AddString(tempStrKey);
int npos = tempStr2.Find('=');
strKey = tempStr2.Left(npos);
strValue = tempStr2.Mid(npos + 1);

int nItemRow = m_lcShow.GetItemCount();
int nIndex =m_lcShow.InsertItem(nItemRow, strKey);
m_lcShow.SetItemText(nIndex, 1, tempStr1);
m_lcShow.SetItemText(nIndex, 2, strValue);
}
else if (Section[j] == '\0')
{
if (Section[j+1] == '\0')
break;//两个\0表示结束

tempStr2 = Section + j +1;
int npos = tempStr2.Find('=');
strKey = tempStr2.Left(npos);
strValue = tempStr2.Mid(npos + 1);

int nItemRow = m_lcShow.GetItemCount();
int nIndex =m_lcShow.InsertItem(nItemRow, strKey);
m_lcShow.SetItemText(nIndex, 1, tempStr1);
m_lcShow.SetItemText(nIndex, 2, strValue);
}
}
}
else if (SectionNames[i] == '\0')
{
if (SectionNames[i+1] == '\0')
break;//两个\0表示结束

tempStr1 = SectionNames + i +1;
m_cbCourse.AddString(tempStr1);
GetPrivateProfileSection(tempStr1, Section, MAX_PATH, L".\\mySyllabus.ini");
for (int j = 0; j < MAX_PATH - 2; j++)
{
if (0 == j && Section[j] != '\0')
{
tempStr2 = Section;
//m_cbCourse.AddString(tempStrKey);
int npos = tempStr2.Find('=');
strKey = tempStr2.Left(npos);
strValue = tempStr2.Mid(npos + 1);

int nItemRow = m_lcShow.GetItemCount();
int nIndex =m_lcShow.InsertItem(nItemRow, strKey);
m_lcShow.SetItemText(nIndex, 1, tempStr1);
m_lcShow.SetItemText(nIndex, 2, strValue);
}
else if (Section[j] == '\0')
{
if (Section[j+1] == '\0')
break;//两个\0表示结束

tempStr2 = Section + j +1;
int npos = tempStr2.Find('=');
strKey = tempStr2.Left(npos);
strValue = tempStr2.Mid(npos + 1);

int nItemRow = m_lcShow.GetItemCount();
int nIndex =m_lcShow.InsertItem(nItemRow, strKey);
m_lcShow.SetItemText(nIndex, 1, tempStr1);
m_lcShow.SetItemText(nIndex, 2, strValue);
}
}
}

#4


引用 2 楼 VisualEleven 的回复:
int nItemRow = m_lcShow.GetItemCount();
                m_lcShow.InsertItem(nItemRow, _T(""));
               m_lcShow.SetItemText(nItemRow , 0, strKey);
                m_lcShow.SetItemText(nItemRow , 1, tempStr1);
                m_lcShow.SetItemText(nItemRow , 2, strValue);

为什么这这么写呢,和我写的区别是什么?

#5


引用 1 楼 lx624909677 的回复:
遇到的问题是什么?

问题解决了,贴在下 面,你看到我那个显示有点问题了吧,重用相似逻辑的代码没修改好。

#1


遇到的问题是什么?

#2


int nItemRow = m_lcShow.GetItemCount();
                m_lcShow.InsertItem(nItemRow, _T(""));
               m_lcShow.SetItemText(nItemRow , 0, strKey);
                m_lcShow.SetItemText(nItemRow , 1, tempStr1);
                m_lcShow.SetItemText(nItemRow , 2, strValue);

#3


昨天回去自己解决了,可能有不到位的地方,麻烦大家指正
CString tempStr1, tempStr2, strKey, strValue;
//tempStr1,tempStr2,strKey, strValue分别存放SectionName,键值对,键名,值
TCHAR SectionNames[MAX_PATH],Section[MAX_PATH];
ZeroMemory(SectionNames, MAX_PATH);
ZeroMemory(Section, MAX_PATH);

GetPrivateProfileSectionNames(SectionNames, MAX_PATH, L".\\mySyllabus.ini");

for (int i = 0; i < MAX_PATH - 2; i++)
{
if (0 == i && SectionNames[i] != '\0')
{
tempStr1 = SectionNames;
m_cbCourse.AddString(tempStr1);
GetPrivateProfileSection(tempStr1, Section, MAX_PATH, L".\\mySyllabus.ini");
for (int j = 0; j < MAX_PATH - 2; j++)
{
if (0 == j && Section[j] != '\0')
{
tempStr2 = Section;
//m_cbCourse.AddString(tempStrKey);
int npos = tempStr2.Find('=');
strKey = tempStr2.Left(npos);
strValue = tempStr2.Mid(npos + 1);

int nItemRow = m_lcShow.GetItemCount();
int nIndex =m_lcShow.InsertItem(nItemRow, strKey);
m_lcShow.SetItemText(nIndex, 1, tempStr1);
m_lcShow.SetItemText(nIndex, 2, strValue);
}
else if (Section[j] == '\0')
{
if (Section[j+1] == '\0')
break;//两个\0表示结束

tempStr2 = Section + j +1;
int npos = tempStr2.Find('=');
strKey = tempStr2.Left(npos);
strValue = tempStr2.Mid(npos + 1);

int nItemRow = m_lcShow.GetItemCount();
int nIndex =m_lcShow.InsertItem(nItemRow, strKey);
m_lcShow.SetItemText(nIndex, 1, tempStr1);
m_lcShow.SetItemText(nIndex, 2, strValue);
}
}
}
else if (SectionNames[i] == '\0')
{
if (SectionNames[i+1] == '\0')
break;//两个\0表示结束

tempStr1 = SectionNames + i +1;
m_cbCourse.AddString(tempStr1);
GetPrivateProfileSection(tempStr1, Section, MAX_PATH, L".\\mySyllabus.ini");
for (int j = 0; j < MAX_PATH - 2; j++)
{
if (0 == j && Section[j] != '\0')
{
tempStr2 = Section;
//m_cbCourse.AddString(tempStrKey);
int npos = tempStr2.Find('=');
strKey = tempStr2.Left(npos);
strValue = tempStr2.Mid(npos + 1);

int nItemRow = m_lcShow.GetItemCount();
int nIndex =m_lcShow.InsertItem(nItemRow, strKey);
m_lcShow.SetItemText(nIndex, 1, tempStr1);
m_lcShow.SetItemText(nIndex, 2, strValue);
}
else if (Section[j] == '\0')
{
if (Section[j+1] == '\0')
break;//两个\0表示结束

tempStr2 = Section + j +1;
int npos = tempStr2.Find('=');
strKey = tempStr2.Left(npos);
strValue = tempStr2.Mid(npos + 1);

int nItemRow = m_lcShow.GetItemCount();
int nIndex =m_lcShow.InsertItem(nItemRow, strKey);
m_lcShow.SetItemText(nIndex, 1, tempStr1);
m_lcShow.SetItemText(nIndex, 2, strValue);
}
}
}

#4


引用 2 楼 VisualEleven 的回复:
int nItemRow = m_lcShow.GetItemCount();
                m_lcShow.InsertItem(nItemRow, _T(""));
               m_lcShow.SetItemText(nItemRow , 0, strKey);
                m_lcShow.SetItemText(nItemRow , 1, tempStr1);
                m_lcShow.SetItemText(nItemRow , 2, strValue);

为什么这这么写呢,和我写的区别是什么?

#5


引用 1 楼 lx624909677 的回复:
遇到的问题是什么?

问题解决了,贴在下 面,你看到我那个显示有点问题了吧,重用相似逻辑的代码没修改好。