Std :: stringstream and std :: ios :: binary

I want to write std::stringstreamwithout any conversions, such as line endings.

I have the following code:

void decrypt(std::istream& input, std::ostream& output)
{
    while (input.good())
    {
        char c = input.get()
        c ^= mask;
        output.put(c);

        if (output.bad())
        {
            throw std::runtime_error("Output to stream failed.");
        }
    }
}

The following code works like a charm:

std::ifstream input("foo.enc", std::ios::binary);
std::ofstream output("foo.txt", std::ios::binary);
decrypt(input, output);

If I use the following code, I run in std::runtime_errorwhere the output is in an error state.

std::ifstream input("foo.enc", std::ios::binary);
std::stringstream output(std::ios::binary);
decrypt(input, output);

If I delete std::ios::binary, the decryption function completes without errors, but in the end I get CR, CR, LF as line endings.

I am using VS2008 and have not tested gcc code yet. Is this how it should behave or is the execution of MS std::stringstreambroken?

Any ideas how I can get the content in std::stringstreamthe correct format? I tried putting the contents in std::stringand then using write(), and it also had the same result.

+5
2

AFAIK, binary fstream, stringstream , .

, , stringstream ctor, in, out . out (, , ostringstream) , , .

stringstream "mode" ctor in|out, , , - .

+11

std::stringstream output(std::stringstream::out|std::stringstream::binary);
-2

All Articles