But if you read the MSDN page on fopen() that's exactly what the "wb" parameter should prevent, but it still doesn't do what it's supposed to do. The resulting file is identical if you open with "w", "wt" or "wb".
Main Topics
Browse All TopicsUsing 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[])
{
FILE *fp;
fp = fopen ("crlf.tmp", "wb");
fwrite ("abc\n", 1, 4, fp);
fputc ('x', fp);
fputc ('y', fp);
fputc ('z', fp);
fputc ('\n', fp);
fclose (fp);
return 0;
}
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.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Well, the warnings depend on the type of project. If I created an empty project, and pasted in my code, I got no warnings at all Did this test after my origianl post.
At first, I created a Win32 console app with a sample application. Which generates one of those stdafx projects. It's never been clear to me what that stuff is doing, but anyway, in that case it complains about a lot more security related stuff which you can solve with this pre-compiler marcro.
But I only mentioned this to be complete. I don't think it's related. I just started from scratch with a "create empty project", built the app, no warnings whatsoever, and still each linefeed gets translated into a "cr-lf". Go figure ..... :-)
I ran the same code in a cpp win32 console project, no compile errors and it generated a the file without any CR only the LF character.
What hex tool did you use to see if there are any CR's in the file? The resulting filesize is 8 bytes. What do you see as the filesize? (6 bytes for the printable ascii and two newlines).
Anthony, seems you nailed it.
To view the Hex code, I opened the file in UltraEdit and then switched to HEX mode. Seems that UE is first converting the LF's into CR-LF's (I presume that's configurable :-) while reading in the file.
I ftp-ed the file to a Unix box (in binary mode) and used 'od -x' to verify. Indeed no CRs that way.
Thanks,
Willem
>> Seems that UE is first converting the LF's into CR-LF's
That's indeed what UE does (you should get a pop-up asking whether you want to convert the file to DOS format - if you click OK, it will do this replacement).
There are several hex editors for Windows that don't have this "problem", as long as you pick one that is advertised as a hex editor. UltraEdit is a text editor that just happens to have a hex functionality, but it is first and foremost a test editor.
In Windows, I have been using frhed. http://www.codeproject.com
Business Accounts
Answer for Membership
by: avizitPosted on 2007-08-07 at 19:50:14ID: 19651072
This is prolly because newline in win machines are translated as CR-LF pair.
on unix machines you can usually convert the file to use only \n using the dos2unix command if available
dos2unix filename