c++ 读写复合文件简单例子

创建个MFC的简单功能来调用以下接口

void testWrite()
{
	// TODO: Add your control notification handler code here
	const unsigned short *FILENAME = L"c:\test.stg";
	DWORD dwMode=STGM_WRITE|STGM_CREATE|STGM_SHARE_EXCLUSIVE;	// 只写|如果文件存在则替换,不存在则创建|独占
	IStorage *pStgRoot,*pStgSub;
	IStream *pStream;
	pStgRoot=NULL;	// 设为NULL,可以知道函数是否执行成功
	////////////////////////////////////////////////////////////////////////////////
	// 创建复合文档
	// 如果文件不存在则创建一个新文件,已经存在则替换原文件
	// 创建完成后得到一个新的storage对象,可用于创建一个根目录
	StgCreateDocfile(
		FILENAME,	// 复合文档路径
		dwMode,	// 请问模式
		0,	// 必须为0
		&pStgRoot	// 返回一个新的storage对象
		);
	////////////////////////////////////////////////////////////////////////////////
	// 创建一个根目录,并准备创建一个子目录
	pStgRoot->CreateStorage(
		L"StgRoot",	// 子IStorage的名称
		dwMode,	// 请问模式
		0,	// 必须为0
		0,	// 必须为0
		&pStgSub	// 返回一个新的storage对象
		);
	////////////////////////////////////////////////////////////////////////////////
	// 创建一个数据流(结点),并准备写入数据
	pStgSub->CreateStream(
		L"Stm",	// 子IStream的名称
		dwMode,	// 请问模式
		0,	// 必须为0
		0,	// 必须为0
		&pStream	// 返回一个新的IStream接口指针
		);
	////////////////////////////////////////////////////////////////////////////////
	// 写入数据
	char strText[]={"Hello World2!"};
	ULONG actWrite;
	int nLen=strlen(strText);	// 字串长度
	pStream->Write(
		strText,	// 要写入的数据
		nLen,	// 要写入数据的长度
		&actWrite	// 实际写入长度,也可以设为NULL
		);
	////////////////////////////////////////////////////////////////////////////////
	// 释放资源
	// 如果不调用Release()方法,会导致写入不完全、文档体积大、无法正常读取等问题
	pStream->Release();
	pStgSub->Release();
	pStgRoot->Release();
	////////////////////////////////////////////////////////////////////////////////
	CString strMsg;
	strMsg.Format("数据长度:%d , 实际写入长度:%ld",nLen,actWrite);
	AfxMessageBox(strMsg);
	cout<<strMsg.GetBuffer(100)<<endl;
}

void testRead()
{
	DWORD dwMode=STGM_READ|STGM_SHARE_EXCLUSIVE;	// 只读|独占
	const unsigned short *FILENAME = L"c:\test.stg";
	IStorage *pStgRoot,*pStgSub;
	IStream *pStream;
	pStgRoot=NULL;
	pStgSub=NULL;
	pStream=NULL;
	////////////////////////////////////////////////////////////////////////////////
	// 打开文件
	StgOpenStorage(
		FILENAME,
		NULL,
		dwMode,
		NULL,
		0,
		&pStgRoot
		);
	////////////////////////////////////////////////////////////////////////////////
	// 打开一个目录
	pStgRoot->OpenStorage(
		L"StgRoot",	// 注意大小写
		NULL,
		dwMode,
		NULL,
		0,
		&pStgSub
		);
	////////////////////////////////////////////////////////////////////////////////
	// 准备读取数据
	pStgSub->OpenStream(
		L"Stm",
		NULL,
		dwMode,
		0,
		&pStream
		);
	////////////////////////////////////////////////////////////////////////////////
	// 读取数据
	const int nLen=255;	// 准备读入的长度
	char strText[nLen]={0};	// 必须指定初始值,否则会显示出乱码,也可以设为 ''
	ULONG actRead;
	pStream->Read(
		strText,	// 存放放入的数据的缓冲区
		nLen,	// 要读入数据的长度,如不清楚可以设为较大的数
		&actRead	// 实际读入的长度
		);
	////////////////////////////////////////////////////////////////////////////////
	// 释放资源
	pStream->Release();
	pStgSub->Release();
	pStgRoot->Release();
	////////////////////////////////////////////////////////////////////////////////
	AfxMessageBox(strText);
	CString strMsg;
	strMsg.Format("指定读入数据的长度:%d , 实际读入数据长度:%ld",nLen,actRead);
	AfxMessageBox(strMsg);
}
原文地址:https://www.cnblogs.com/x-ing/p/3468364.html