Search code examples
c++fstreamcout

c++ cout instead of fstream


Normally I live in my guarded world of C#. But sometimes I have to break out and do something outside.

At the moment I have to decode an audiostream and have to output this directly in my c++ console application.

If I write the content into a file, I can hear the correct result. But if I use instead of a fstream cout, I get only a noisy sound.

How I have to do it correct? Here the working filestream code:

fstream wavefile;
wavefile.open(output, ios::out | ios::binary | ios::trunc);

//do something
wavefile.write((char*) &waveheader, waveheadersize);
//do something else
do {
        //do something
        //decodedBuffer is of type BYTE* , decodedLength is of type DWORD
        wavefile.write((char*) decodedBuffer, decodedLength);
        wavefile.flush();
    } while (encodedLength > 0);

My not working cout code:

std::cout.setf(ios::out | ios::binary | ios::trunc);

//do something
//this works, I got the same output
cout << structToString(&waveheader) << endl;
//do something else
do {
        //do something
        cout << (char *)decodedBuffer;
    } while (encodedLength > 0);

Thanks in advance


Solution

  • First, there is absolutely no reason to use different code for a std::fstream and for std::cout (Beside which, your cout-code uses formatted output, and wrong):

    You are using their ostream-interface in both cases.

    So, first things first, repair the second code (as far as possible) by replacing it with the first.


    Now, we come to the one difference (which you tried to paper over with setf): std::cout is in text-mode, not binary mode!

    Unfortunately, none of ios::out, ios::binary and ios::trunc are formatting-flags, so you cannot set them with setf.

    Actually, the mode cannot be changed at all after the fact (at least not portably).

    Fortunately, you can simply ignore having the wrong mode on many systems, as Linux and others equate textmode and binary-mode. On windows, this hack should get you around it:

    cout.flush();
    fflush(stdout);
    _setmode(_fileno(stdout), _O_BINARY);