Why do I get corrupt output on my file?

Posted by Martin on Stack Overflow See other posts from Stack Overflow or by Martin
Published on 2010-06-08T22:08:51Z Indexed on 2010/06/08 22:12 UTC
Read the original article Hit count: 218

Filed under:
|
|
|

I have a simple program which I have compiled in both MinGW and Visual C++ 2008 Express, and both give an output file larger than 88200. When I set s = 0, both programs work as expected. What am I doing wrong?

#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    int i;
    short s;

    fstream f;

    f.open("test.raw", ios_base::out);

    for(i = 0; i < 44100; i++)
    {
        s = i & 0xFFFF; // PROBLEM?
        f.write(reinterpret_cast<const char *>(&s), sizeof(s));
    }

    f.close();

    return 0;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about int