fopen ("file", "wb") still writes cf-lf text files in VS2005
Posted on 2007-08-07
Using Visual Studio 2005 in pure C (no C++) I have problems with writing a binary file using fopen() and fputc(). Here is my little test program:
int main (int argc, char* argv)
fp = fopen ("crlf.tmp", "wb");
fwrite ("abc\n", 1, 4, fp);
fputc ('x', fp);
fputc ('y', fp);
fputc ('z', fp);
fputc ('\n', fp);
When you look at the resulting file in a hex editor, in both cases the linefeeds are translated into a CR-LF pair. Which would be OK if the file was opened as default or as a text-file but not when it is opened in binary mode.
This has for me been an ongoing problem. Last time I circumvented it by replacing my C-style file I/O with MS specific calls like CreateFile() and ReadFile(). But this time I prefer to solve the problem "once and for all". :) Also I need this code to be platform independent and this problem doesn't occur on my Unix systems.