>From: Zhang Le <ejoy@...>
>To: Benjamin Riefenstahl <Benjamin.Riefenstahl@...>
>Subject: Re: [Mingw-users] binary file stream cause data lose on win32?
>Date: Mon, 14 Oct 2002 10:05:56 +0800
>On Sunday 13 October 2002 21:56, you wrote:
> > That is good practice, but should not be necessary. If this actually
> > doesn't work without explicit f.close(), that would be a bug.
>I think so too.Under linux,all things are right,but when porting to win32,
>some program is right while some are not.Hope it's my program's bug.
> > > 2.always use ofstream(file,ios::binary) or fopen(file,"wb") to open
> > > a binary for written,even it not necessary under Linux.
> > Right. Linux and Unix don't have the distinction text vs. binary.
> > For other systems it's rather important.
>Would it be possible to make mingw32 workaround this for easy porting of
Probably not in the way you imagine, and Mingw is not primarily a tool for
porting Unix software to Windows. That is what Cygwin is for, and as such it
provides ways of dealing with text vs. binary file I/O. However, in Mingw
you set the global variable _fmode to open files in binary mode by default:
_fmode = _O_BINARY;
This is declared in fcntl.h. Or you can instead _define_ this global
variable in your program:
unsigned int _CRT_fmode = _O_BINARY;
This will have the same effect, plus the standard files stdin, stdout and
stderr will also be opened in binary mode. Note that _fmode will work with
MSVC but _CRT_fmode is specific to Mingw.
> > The problem is just missing C++ education (no offense intended). Any
> > book or documentation that teaches binary streams in C++ and doesn't
> > mention the issue should be thrown away.
> > PS: Note that with binary streams you are not even using any ofstream
> > features (that "f" is for "formatted" == "text" after all), so you can
> > just as well directly use the underlying
> > filebuf f;
> > f.open("file.bin",ios::binary|ios::out);
> > f.sputn((char*)dataptr,datasize);
> > f.close();
>Maybe my unpleasent experience worthy of a hint for unix->win32
>many unix developers do not notice such difference.
>Thanks a lot.
> Sincerely yours,
> Zhang Le
Join the worlds largest e-mail service with MSN Hotmail.