将文件复制到流 [英] copying file to Stream

查看:87
本文介绍了将文件复制到流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我做错了,请纠正我.根据结构化存储,存储对应于目录/文件夹,而流对应于文件.
现在使用结构化存储概念,我使用扩展名为.stg的StgCreateDocfile()创建了一个存储.我已经使用IStream-> CreateStream()在其中创建了一个Stream.
我想添加我的硬盘文件.此创建的Storage(.stg)流的Abstract.doc.我该怎么做?

我尝试使用IStream :: Write方法

Correct me if im wrong. As per Structured Storage, Storage corresponds to Directory/folder and Stream corresponds to file.
Now Using Structured Storage concept, I have created a storage using StgCreateDocfile() with .stg extension. I have created a Stream inside it using IStream->CreateStream().
I want add my Hard disk file eg. Abstract.doc to this stream of created Storage(.stg). How can I do that??

I tried it using IStream::Write method

HRESULT Write( 
  void const* pv, 
  ULONG cb,
  ULONG* pcbWritten 
);



可以将指针pv指向我的硬盘文件的直接指针吗?在这里abstract.doc而不是Buffer?

预先感谢您....



can pointer pv be the direct pointer to my hard disk file eg. here abstract.doc instead of Buffer?

Thanking you in advance....

推荐答案

是的,您是对的.你可以这样考虑.
您需要将文件内容读入缓冲区(部分或全部).然后调用IStream :: Write().
Yes you are right. You can consider like that.
You need to read your file content into a buffer (partially or as a whole). Then you call IStream::Write().
#define BUFFERSIZE 4096  // (edit)

unsigned char * pBuffer = new unsigned char [BUFFERSIZE];

HANDLE hFile = CreateFile(pszFileName, ...);

// you can determine file size and set initially
// pStream->SetSize(...)

DWORD dwRead = 0;
ULONG ulWritten = 0;
do
{
    if(!ReadFile(hFile, pBuffer, BUFFERSIZE, &dwRead, NULL))
        break;

    if(0 == dwRead) // means EOF
        break;

    pStream->Write(pBuffer, dwRead, &ulWritten);

    // you can also check (ulWritten == dwRead) just to make sure

} while(BUFFERSIZE == dwRead);    // be careful (edit)

CloseFile(hFile);

delete [] pBuffer;


这篇关于将文件复制到流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆