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

.NET Question

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.


Default Text
 

Verified Answer?

The member who asked this question verified this comment provided the solution that solved their problem.

by:Posted on 2007-08-07 at 20:44:37ID: 19651302

This content is available to Experts Exchange members

See the answer now
with your Free 14 Day Trial

Get unlimited access to solutions & experts

  • 4,169,477 solved questions
  • 3,805 articles & videos
  • 15,413 tech experts

Get Access Now

Trusted by Thousands of Top Companies

  • CVS
  • IBM
  • Boeing
  • US Army
  • Kaiser
  • RalphLauren
  • Xerox
  • Cox
  • BlueCross
  • Accenture
  • Sony
  • Toyota
  • Allstate
  • Att
  • TomTom
  • BBC
  • Raytheon
  • Wells Fargo
  • US Navy
  • Direct TV
  • FootLocker
  • Nikon
  • BritishCouncil
  • TS Army
201504-LO-Qu-049

Related Questions We have nearly 4 million solutions here.

See More .NET Solutions

Experts Exchange powers the growth and success
of technology professionals worldwide.

  • Solve

    Experts Exchange is the tech professional’s trusted, on-demand resource for solving difficult problems, making informed decisions, and delivering excellent solutions.

  • Learn

    With unparalleled access to technical experts, verified real-world solutions, and diverse educational content, Experts Exchange enables personalized development of technology skills.

  • Network

    Experts Exchange gives you the professional exposure and valued relationships key to building the career you want.

Join the Network Today

See Plans and Pricing