This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Impossible to do binary output? (no, std::ios::binary is not the solution)
- To: libstdc++ at gcc dot gnu dot org
- Subject: Impossible to do binary output? (no, std::ios::binary is not the solution)
- From: Johan Johansson <johanj at acm dot org>
- Date: Sun, 03 Jun 2001 18:19:44 +0200
Hi.
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:
http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#3
.....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?
j
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]