1

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 { ... }
Mihai Todor
  • 8,014
  • 9
  • 49
  • 86

1 Answers1

1

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...

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • I don't want to save the file to disk, though. That was just for testing. I'll try to extract the std::string as you suggested and see if it works. Thanks! – Mihai Todor Aug 03 '12 at 16:16