Thousands of experts available around the clock to solve your toughest technology problems.

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

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.


201407-LO-Qu-006

Unlock thousands of answers and tutorials instantly!

Try it Free

30 day free trial. Cancel anytime.

Learn More about HOW IT WORKS

Get Experts Exchange's

Career Builder Guide

Become successful in your tech career.

We will never share this with anyone.

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