One of the most common problems when porting file I/O code from other operating systems to Windows is that the C runtime on Windows allows files to be opened in two modes: text and binary. This distinction originated because text files on Windows use a carriage return ('\r') and a linefeed ('\n') to indicate a newline, while on Unix only a linefeed is used. To ensure that ANSI C requirements are met, the C runtime translates CR+LF line endings to LF when reading and writing text files. However, if your program operates on binary files (e.g. an executable) then this translation must be disabled by opening the file in binary mode, for example:

  FILE *fp = fopen("filename.abc", "rb");

The "b" option is ANSI C compliant, so this code will work on Unix too. The main difficulty with porting is that text mode is the default so any program that works on binary files must be modified. Instead of modifying every call to fopen(), there are Windows-specific ways to make binary mode the default:

1) You could add /mingw/lib/binmode.o to the objects when you build the executable. This has the effect of making binary-mode file translation the default (except that preopened files stdin, stderr, stdout remain text).

2) Another way is to:

#include <stdlib.h> /*  _fmode */
#include <fcntl.h> /*  _O_BINARY */

int main()
{
...
   _fmode = _O_BINARY;
...
}

3) Yet another way:

#include <stdlib.h> /*  _fmode */
#include <fcntl.h> /*  _O_BINARY */

#undef _fmode
int _fmode = _O_BINARY;

main ()
{
}

4) Yet again another way:

#include <stdlib.h> /* _fmode */
#include <fcntl.h> /*  _O_BINARY */

unsigned int _CRT_fmode = _O_BINARY;

main ()
{
}

Setting _CRT_fmode also has the effect of making stdin. stderr, stdout binary mode which is useful if you are redirecting them to files.

If you have changed the default to binary mode, you can still open a file as text as follows:

  FILE *fp = fopen("filename.txt", "rt");

However, this is not ANSI C compliant. As a long term solution though, it is better to modify each call to fopen(), and this also allows a mixture of binary and text files to be used in a single program in an ANSI C compliant way.

5) Yet again another way:

#include <stdio.h>
#include <fcntl.h>
...
int main(...) {
#if _WIN32
        _setmode(_fileno(stdin), _O_BINARY);
        _setmode(_fileno(stdout), _O_BINARY);
        _setmode(_fileno(stderr), _O_BINARY);
#endif

References:

http://msdn.microsoft.com/library/en-us/vclib/html/_crt_Text_and_Binary_Mode_File_I.2f.O.asp http://msdn.microsoft.com/library/en-us/vclib/html/_crt_fopen.2c_._wfopen.asp http://msdn.microsoft.com/library/en-us/vclib/html/_crt__fmode.asp http://msdn.microsoft.com/library/en-us/vclib/html/_crt__open.2c_._wopen.asp


You will also want to force binary mode if you need to rely on the in-memory representation of the file being the same as that on disk. For example to seek() to a location found in a string read from file. However it is probably good practice not to do so. -- SteveLee

PHP Warning

lib/XmlElement.php:43 Warning[2]: get_class() expects parameter 1 to be object, string given (...repeated 9 times)

lib/XmlElement.php (In template 'navbar' < 'top' < 'body' < 'html'):43 Warning[2]: get_class() expects parameter 1 to be object, string given:

lib/XmlElement.php (In template 'navbar' < 'top' < 'body' < 'html'):43 Warning[2]: get_class() expects parameter 1 to be object, string given:

lib/XmlElement.php (In template 'browse' < 'body' < 'html'):43 Warning[2]: get_class() expects parameter 1 to be object, string given

Valid XHTML 1.0! Valid CSS!
Page Execution took real: 0.085, user: 0.040, sys: 0.010 seconds , Memory: 2483080

lib/XmlElement.php (In template 'actionbar' < 'browse-footer'):43 Warning[2]: get_class() expects parameter 1 to be object, string given:

lib/XmlElement.php (In template 'actionbar' < 'browse-footer'):43 Warning[2]: get_class() expects parameter 1 to be object, string given: