Solved

fopen ("file", "wb") still writes cf-lf text files in VS2005

Posted on 2007-08-07
13
1,625 Views
Last Modified: 2013-11-26
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[])
{
      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.


0
Comment
Question by:wschaik
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 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  
0
 

Author Comment

by:wschaik
ID: 19651101
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".
0
 
LVL 13

Expert Comment

by:josgood
ID: 19651119
I tried your program with VS205 in a C++ project and a C project.  In both cases, the '\n' appears as 0x0a in the hex file.  I cut-and-pasted your code.  I don't get why I'm getting different results.

Could this be a setting in VS2005?
0
 

Author Comment

by:wschaik
ID: 19651153
Hi josgood, that's what I was wondering as well. However, to test this I created a 100% fresh project (Win32 console application). I did define _CRT_SECURE_NO_DEPRECATE in my project properties to get rid of compiler warnings, but I can't see how that makes a difference.
0
 
LVL 13

Expert Comment

by:josgood
ID: 19651177
I had no compiler warnings.  This is curiouser and curiouser.

I checked f_mode on my machine.  Setting it to _O_TEXT,  _O_BINARY and _O_WTEXT made no difference.
0
 
LVL 13

Expert Comment

by:josgood
ID: 19651189
I'm running Visual Studio Pro
Version 8.0.50727.762 (SP.050727-7600)

Probably not relevant, but you think of service packs.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 11

Expert Comment

by:avizit
ID: 19651205
what compiler warnings you are getting ? your code looks pretty straight forward  and should compile cleanly
0
 

Author Comment

by:wschaik
ID: 19651297
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 ..... :-)

0
 
LVL 8

Accepted Solution

by:
Anthony2000 earned 125 total points
ID: 19651302
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).

0
 

Author Comment

by:wschaik
ID: 19651351
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
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19651657
>> 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.
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 19653496
In Windows, I have been using frhed. http://www.codeproject.com/tools/frhed.asp
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 19657427
Hi Willem,

In UltraEdit, go to 'Advanced' -> 'Configuration...'. On the 'General' tab find 'Load/Save/Conversions' and adjust the 'Unix/Mac file detection/conversion' setting.

That applies to version 10 but this mechanism has always been present.

Paul
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Memory Usage 2 49
Computer slow / BSOD 10 38
Learning Processes involved in building a asp.net mvc (C#)  application that uses an Oracle database 8 71
How to learn Linux? 10 42
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now