0
点赞
收藏
分享

微信扫一扫

《计算机网络简易速速上手小册》第2章:计算机网络协议和标准(2024 最新版)

雪域迷影 2024-02-06 阅读 29

先定义一个结构体;

struct myTxc
{
    char c;
    CString name;
    int value;
} txc;

读和写的菜单代码;

void CjgtrwView::On32771()
{
	// TODO: 在此添加命令处理程序代码
	CFile file(_T("test1.txt"), CFile::modeCreate | CFile::modeWrite);
	txc.name = _T("测试一");
	txc.value = 999;
	txc.c = 't';
	//fwrite(&sa, sizeof(sa), 1, fp);
	file.Write(&txc, sizeof(txc));
	file.Close();
}

void CjgtrwView::On32772()
{
	// TODO: 在此添加命令处理程序代码
	CString str1;
	CFile file(_T("test1.txt"),CFile::modeRead);
	//fread(&sb,sizeof(sb),1,fp);
	myTxc txc2;
	file.Read(&txc2, sizeof(txc2));
	
	CClientDC dc(this);
	dc.TextOutW(20, 20, txc2.name);
	str1.Format(_T("%d"), txc2.value);
	dc.TextOutW(20, 50, str1);
	str1.Format(_T("%c"), txc2.c);
	dc.TextOutW(20, 80, str1);

	file.Close();
}

先给结构体变量赋值,然后写入文件;再读取,并显示;

保存的是二进制格式,打开文件查看是乱码; 

举报

相关推荐

0 条评论