extra bytes being written to a file

It's been a while since I used the cstdio library. Is there something wrong with this code? For some reason, it works fine up to 48 bytes but if I write 64 bytes I end up with a file that has more than 64 bytes. I don't see how that can be. Can someone compile and run this with their win32 compiler and let me know if it works okay?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdio>
#include <algorithm>
#include <string>

int main(int argc, char **argv)
{
    // Write some data to the file so that we can test the read and seek functions.
    const unsigned int NUM_BYTES(64);
    char writeBuffer[NUM_BYTES];
    std::string fileName = "C:\\AosDomainTest.bin";
    FILE* fp = fopen(fileName.c_str(), "w+");
    std::generate(writeBuffer, writeBuffer + NUM_BYTES, rand);
    fwrite(writeBuffer, 1, NUM_BYTES, fp);
    // Close the file now so that the AOS domain can access it.
    fclose(fp);

    return 0;
}

You need to open the file in Binary mode. Text mode (default) changes line breaks.

This was a really poor decision by the langauge to make text mode by default.

 
FILE* fp = fopen( fileName.c_str(), "w+b" );  //<-- note the 'b' for binary 
Ah, yes. That's it. Thanks Disch.
Topic archived. No new replies allowed.