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

标签 c++ c istream

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

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

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

最佳答案

根据您在此处提供的 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();
}

关于c++ - 如何将 IStream 的内容转储(写入)到文件中(图片),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25944011/

相关文章:

c - 什么可能导致 getaddrinfo() 返回错误代码 "1"?

c++ - C++ 中的 istream::unget() 没有像我想的那样工作

没有输入时C++ getline不为空

c++ - 这是 getline() 的错误,还是我做错了什么。 getline() 的正确使用方法?

c++ - MSVC查找符号来自何处

c++ - 使用 double 值的货币格式

c++ - 为什么 system() 提示 cwd 未知?

c++ - 访问文件和打开文件有什么区别

c - 当 630 反转 36 时打印。为什么不显示0?

c - 如何在gdb中的工作目录外设置断点