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.


201409-LO-Qu-015

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

Try for Free

30 day free trial. Cancel anytime.

Learn More about How It Works

Related Questions Need more help?
We have over 4 million solutions here.

See More Microsoft Visual C++.Net Solutions

Deeper Microsoft Visual C++.Net LearningDevelop your expertise with tips, tricks and how to expert articles.

See More Microsoft Visual C++.Net Articles

Skill Training Video TutorialsExpand your tech skills quickly with five-minute, instructional videos.

See More Programming Videos

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