如何将IStream的内容转储(写入)到文件(图像)

问题描述 投票:3回答:1

我有一个IStream,我知道它包含一个PNG文件,但是我无法将其内容像普通I / O流一样写入文件,我不知道我做错了还是应该做将IStream写入文件的另一件事。

    IStream *imageStream;
    std::wstring imageName;
    packager.ReadPackage(imageStream, &imageName);      

    std::ofstream test("mypic.png");
    test<< imageStream;
c++ c istream
1个回答
4
投票

基于您在此处提供的IStream参考,是一些unested代码,它们应该可以大致执行您想要的操作:

void output_image(IStream* imageStream, const std::string& file_name)
{
    std::ofstream ofs(file_name, std::ios::binary); // binary mode!!

    char buffer[1024]; // temporary transfer buffer

    ULONG pcbRead; // number of bytes actually read

    // keep going as long as read was successful and we have data to write
    while(imageStream->Read(buffer, sizeof(buffer), &pcbRead) == S_OK && pcbRead > 0)
    {
        ofs.write(buffer, pcbRead);
    }

    ofs.close();
}
© www.soinside.com 2019 - 2024. All rights reserved.