MFC list控件 从文件中添加保存数据项

MFC listcontral控件 创建和添加数据
上一篇文章讲了如何给list空间添加数据,但没有涉及文件操作,list上添加的数据项不能保存,这篇文章将讲解如何将文件中的数据添加到list控件中,已经如何将list控件中的数据保存到文件中。

先添加一个vector容器保存列表中的每一个英雄信息结构体对象,在pch.h中包含头文件

在Dlg类中std::vector heroVector;

向文件写了数据

通过类向导添加WM_DESTROY消息响应函数

void Clist控件Dlg::OnDestroy()
{
CDialogEx::OnDestroy();

CFile file;
bool can = file.Open(L".\\heroinfo.dat", CFile::modeCreate | CFile::modeWrite);
if (false == can)
{
MessageBox(L"打开失败");
return;
}
int cnt = m_list.GetItemCount();
for (int i = 0; i < cnt; i++)
{
HeroInfo info = { 0 };
wcscpy(info.name, m_list.GetItemText(i, 0));
wcscpy(info.Game_Profession, m_list.GetItemText(i, 1));
info.Price_God = _wtoi(m_list.GetItemText(i, 2));
info.Price_Tickets = _wtoi(m_list.GetItemText(i, 3));
file.Write(&info, sizeof(info));

}
file.Close();
}

获取列表项的文本使用GetItemText方法。

从文件中读取

添加一个函数LoadHeroInfo用来读取文件信息,LoadHeroInfo在OnInitDialog函数中调用。

void Clist控件Dlg::LoadHeroInfo(LPCTSTR filename)
{
// TODO: 在此处添加实现代码.
CFile file;
bool can = file.Open(filename, CFile::modeRead);
if (false == can)
{
MessageBox(L"打开失败");
return;
}
HeroInfo heroInfo = { 0 };
while (file.Read(&heroInfo, sizeof(heroInfo)))
{
                count++;//记录个数的静态全局变量
heroVector.push_back(heroInfo);
InsertHeroInfo(heroInfo);
}
}

添加一个函数InsertHeroInfo实现插入数据项操作

void Clist控件Dlg::InsertHeroInfo(HeroInfo heroInfo)
{
// TODO: 在此处添加实现代码.
int nCnt = m_list.GetItemCount();
m_list.InsertItem(nCnt, heroInfo.name);
m_list.SetItemText(nCnt, 1, heroInfo.Game_Profession);
CString str;
str.Format(L"%d", heroInfo.Price_God);
m_list.SetItemText(nCnt, 2, str);
str.Format(L"%d", heroInfo.Price_Tickets);
m_list.SetItemText(nCnt, 3, str);
}

修改添加按钮的响应函数,在这个函数中还要把添加的结构体对象放到vector容器中

相关内容请参阅:https://zhuanlan.zhihu.com/p/647845255

void Clist控件Dlg::OnBnClickedButton1()
{
UpdateData(true);
HeroInfo hero = { 0 };
wcscpy(hero.name, m_Name.GetBuffer());
wcscpy(hero.Game_Profession, m_Profession.GetBuffer());
hero.Price_God = m_price_gold;
hero.Price_Tickets = m_price_tickets;

heroVector.push_back(hero);//

m_list.InsertItem(count, hero.name);
m_list.SetItemText(count, 1, hero.Game_Profession);
TCHAR str[10] = { 0 };
_itow(hero.Price_God, str,10);
m_list.SetItemText(count, 2, str);
_itow(hero.Price_Tickets, str, 10);
m_list.SetItemText(count, 3, str);
count++;

}

运行

先运行程序添加3个英雄试试

关闭程序再次运行成功读取文件数据

对写入函数进行修改

void Clist控件Dlg::OnDestroy()
{
CDialogEx::OnDestroy();

CFile file;
bool can = file.Open(L".\\heroinfo.dat", CFile::modeCreate | CFile::modeWrite);
if (false == can)
{
MessageBox(L"打开失败");
return;
}
for (auto &it : heroVector)
{
file.Write(&it, sizeof(it));
}
file.Close();
}

因为heroVector容器里已经保存了所有的HeroInfo对象,就可以直接用遍历vector元素的方式保存。

项目链接:https://pan.baidu.com/s/15xxioRFcnTrK9Q_nECLjLA?pwd=us7b

提取码:us7b

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值