【问题标题】:Extracting bytes from byte stream从字节流中提取字节
【发布时间】:2012-08-03 16:09:24
【问题描述】:

我通过 C++ CGI 脚本中的 POST 接收二进制文件,我正在使用 Cgicc 库来获取其内容,如下所示:

std::ofstream myfile;
myfile.open ("file.out", std::ios::in | std::ios::binary);
try
{
    cgicc::Cgicc cgi;
    cgicc::const_file_iterator file = cgi.getFile("bitmap");
    if(file != cgi.getFiles().end())
    {
        file->writeToStream(myfile);
    }
}

catch(std::exception &exception)
{
    std::cout << exception.what();
}

结果是一个包含字节的二进制文件。

现在,因为每个字节应该代表一个 8 位位图文件的一个像素,所以我想构建整个位图文件。为了实现这一点,我想我可以使用easyBMP 库,但由于我需要逐个像素地创建图像,我需要以某种方式迭代接收到的字节。有谁知道如何做到这一点?我可以以某种方式获得一个迭代器到 std::ostream / std::ostrstream / std::ostringstream 吗?

std::ostringstream stream;
file->writeToStream(stream);
//foreach byte in stream do { ... }

【问题讨论】:

    标签: c++ bitmap byte ostream ostringstream


    【解决方案1】:

    如果你使用std::ostringstream,你可以从中得到std::string,使用std::ostringstream::str函数http://cplusplus.com/reference/iostream/ostringstream/str/。此外,您可以打开文件并阅读它...

    【讨论】:

    • 不过,我不想将文件保存到磁盘。那只是为了测试。我将尝试按照您的建议提取 std::string 并查看它是否有效。谢谢!
    猜你喜欢
    • 2019-01-05
    • 2019-01-03
    • 1970-01-01
    • 2015-07-26
    • 2015-04-14
    • 2014-06-24
    • 1970-01-01
    • 2012-04-14
    • 2020-01-11
    相关资源
    最近更新 更多