Writing byte characters to a binary ofstream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blooglet
    New Member
    • Oct 2011
    • 1

    #1

    Writing byte characters to a binary ofstream

    I'm trying to write binary data to a file. I want to create a file that contains the following data (hexadecimal notation):
    80

    So I tried this:
    Code:
        ofstream outFile("file.txt", ios::binary);
        uint32_t character = 0x80;
        outFile << character;
    
        outFile.close();
    Instead, I got...
    3132 380a

    What am I doing wrong?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    YOu need to use outfile.write rather than operator<< for binary. The << writes only text.

    Comment

    Working...