This is the mail archive of the mailing list for the libstdc++ project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Impossible to do binary output? (no, std::ios::binary is not the solution)


As you may have guessed I am trying to write a section of memory byte by
byte to a file. Sounds simple enough, doesn't it?

Attempt 1:

// In class definition
std::ofstream m_outFile;

// In initializer list
m_outFile(fileName, std::ios::binary)

// Actual output (yes, buf is const char*)
m_outFile.write(buf, len);

Result: Every byte with bit 7 set comes out prepended with 0x81 which
looks like some sort of UTF-8 translation to me, e.g. 0x03 0x0ff comes
out as 0x03 0x81 0xff

Since Josuttis' "The C++ Standard Library" like many others is sort of
obfuscated on the issue (at one time says just use binary, which for a
fact doesn't work, and then later goes on to almost explain character
translation on filebufs) I tried scavenging the net and came up with:

.....which leads me to

Attempt 2:

m_outFile.rdbuf()->sputn(buf, len);

Result: Identical to attempt 1

Things that might be of relevance (in order of expected importance):

1. I am in a swedish locale
2. I compiled the program with gcc 2.95.3
3. It was compiled and run on a Linux machine (2.4.5)

Do I really have to fiddle with codecvt<> to do this extremely basic
thing and if so, is setting the same internal and external
representation the thing to do?


       [ Send an empty e-mail to for info ]
       [ about comp.lang.c++.moderated. First time posters: do this! ]

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]