MFC读取文件数据,添加信息到列表并保存到文件

打开并读取文件信息

添加:

复制代码
BOOL infoDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	AfxMessageBox("欢迎查看学生信息");
	SetList();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
void infoDlg::SetList()
{
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_list.InsertColumn(0,_T("学号"),0,100);//0为左对齐,2为居中对齐
m_list.InsertColumn(1,_T("姓名"),0,100);
m_list.InsertColumn(2,_T("性别"),0,100);
m_list.InsertColumn(3,_T("年龄"),2,100);
}
void infoDlg:: showData()//AllData
//{m_list.InsertItem(0,"22001");//第一个不用标注列
//m_list.SetItemText(0,1,"张宇");
//m_list.SetItemText(0,2,"男");
//m_list.SetItemText(0,3,"十九");
//m_list.InsertItem(1,"22002");//第一个不用标注列
//m_list.SetItemText(1,1,"王小慧");
//m_list.SetItemText(1,2,"女");
//m_list.SetItemText(1,3,"十八");
{for(int i=0;i<AllStr.size();i++)//AllStr[i]按行输出
{for(int j=0;j<AllStr[i].size();j++)//写入列表
{if(j==0)
m_list.InsertItem(i,AllStr[i][j].c_str());
else
m_list.SetItemText(i,j,AllStr[i][j].c_str());
}
}
	}

//添加功能
void infoDlg::OnBnClickedButton5()
{CString strno,strname,strsex,strage;
GetDlgItem(IDC_EDIT1)->GetWindowText(strno);
GetDlgItem(IDC_EDIT2)->GetWindowText(strname);
	// TODO: 在此添加控件通知处理程序代码
CButton*p=(CButton*)GetDlgItem(IDC_RADIO1);
	if(p->GetCheck())
	strsex="M";
	else
		strsex="W";

p=(CButton*)GetDlgItem(IDC_CHECK1);
if(p->GetCheck())
	strage+="18";
p=(CButton*)GetDlgItem(IDC_CHECK2);
	int row =m_list.GetItemCount();
	if(p->GetCheck())
	strage+="19";
	m_list.InsertItem(row,strno);
	m_list.SetItemText(row,1,strname);
    m_list.SetItemText(row,2,strsex);
	m_list.SetItemText(row,3,strage);
	CString filename;
CFileDialog mydlg(true,NULL,NULL,NULL,"*.txt||");
if(mydlg.DoModal()==IDOK)
{
//AfxMessageBox("OK");
filename=mydlg.GetPathName();}
else
	return;
	string textlines,item;
	fstream openfile(filename,ios::app);//打开文件流追加数据
	if(openfile.is_open())//检查文件是否成功打开
	{openfile<<strno<<_T(" ")<<strname<<_T(" ")<<strsex<<_T(" ")<<strage<<std::endl;//数据写入文件
	openfile.close();//关闭文件
	}
	else
	{AfxMessageBox("无法打开文件");}

}

//读取文件数据到列表
void infoDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	//string file_path="E:\\data.txt";
CString filename;
CFileDialog mydlg(true,NULL,NULL,NULL,"*.txt||");
if(mydlg.DoModal()==IDOK)
{
//AfxMessageBox("OK");
filename=mydlg.GetPathName();}
else
	return;
	string textlines,item;
	fstream openfile(filename);

	int count=m_list.GetItemCount();
	while(getline(openfile,textlines))//读取每一行到textlines
	{	stringstream items(textlines);
		OneStr.clear();//清空

	while(getline(items,item,' '))
	{
	//AfxMessageBox(item.c_str());
		OneStr.push_back(item);//压入Onestr,读取文件到向量中

	}
	AllStr.push_back(OneStr);//压入AllStr
	
	}
	showData();
}
相关推荐
blasit13 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_2 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星2 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛4 天前
delete又未完全delete
c++
端平入洛5 天前
auto有时不auto
c++
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马5 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝5 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc6 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法