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

时间:2014-09-20 00:17:52

标签: c++ c istream

我有一个IStream我知道它包含一个PNG文件,但是我不能像普通的I / O流那样把它的内容写入文件我不知道我做错了什么或者我应该做什么将IStream写入文件的不同之处。

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

    std::ofstream test("mypic.png");
    test<< imageStream;

1 个答案:

答案 0 :(得分:4)

根据您在此处提供的IStream引用是一些未经测试的代码,应该大致按照您的要求执行:

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();
}