简体   繁体   中英

Extracting bytes from byte stream

I receive a binary file via POST in a C++ CGI script and I'm using the Cgicc library to get its contents like so:

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

The result is a binary file containing the bytes.

Now, because each byte should represent one pixel of an 8 bit bitmap file, I want to construct the entire bitmap file. In order to achieve this, I think I can use the easyBMP library, but since I need to create the image pixel by pixel, I need to somehow iterate over the received bytes. Does anyone know how this can be achieved? Can I get an iterator somehow to an std::ostream / std::ostrstream / std::ostringstream?

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

If you use std::ostringstream you can get std::string from it, using std::ostringstream::str function http://cplusplus.com/reference/iostream/ostringstream/str/ . Also, you can open your file and read it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM