MFC结构体写入文件和读取

先定义一个结构体;

struct myTxc

{

char c;

CString name;

int value;

} txc;

读和写的菜单代码;

cpp 复制代码
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();
}

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

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

相关推荐
xlq223225 小时前
22.多态(上)
开发语言·c++·算法
D_evil__5 小时前
[C++高频精进] 并发编程:线程基础
c++
Mr_WangAndy6 小时前
C++17 新特性_第二章 C++17 语言特性_std::any和string_view
c++·string_view·c++40周年·c++17新特性·c++新特性any
水天需0108 小时前
C++ 三种指针转换深度解析
c++
言言的底层世界8 小时前
c++中STL容器及算法等
开发语言·c++·经验分享·笔记
Mr_WangAndy8 小时前
C++17 新特性_第一章 C++17 语言特性___has_include,u8字符字面量
c++·c++40周年·c++17新特性·__has_include·u8字面量
liu****9 小时前
八.函数递归
c语言·开发语言·数据结构·c++·算法
Vanranrr9 小时前
C++临时对象与悬空指针:一个导致资源加载失败的隐藏陷阱
服务器·c++·算法
BestOrNothing_20159 小时前
【C++基础】Day 5:struct 与 class
c++·c·class类·struct结构体·typename模板·private与public
枫叶丹410 小时前
【Qt开发】Qt窗口(三) -> QStatusBar状态栏
c语言·开发语言·数据库·c++·qt·microsoft