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

    Question by:
    On

    Topics:

    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.


    Good Question?
    0
     

    ?

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

    Accepted Solution on 2007-08-07 at 20:44:37ID: 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).

    Top Expert Contributor

    Essential articles and videos from the Experts

    More valuable questions with Expert answers

    201511-LO-Qu-074

    Extend your technology team with the Experts Exchange community.

    — trusted by —

    Who answers my questions?Our community has technology experts around the world.

    käµfm³d   👽

    9

    Articles

    Expert in:

    • .NET Programming
    • C#
    • Visual Basic.NET
    • ASP.NET
    • Regular Expressions

    Karrtik Iyer

    Expert in:

    • .NET Programming
    • C#
    • Visual Basic.NET
    • ASP.NET
    • C++

    Jacques Bourgeois (James Burger)

    2

    Articles

    Expert in:

    • .NET Programming
    • Visual Basic.NET
    • C#
    • MS Development
    • ASP.NET

    Fernando Soto

    Expert in:

    • .NET Programming
    • Visual Basic.NET
    • C#
    • ASP.NET
    • Editors IDEs

    Alexandre Simões

    6

    Articles

    Expert in:

    • .NET Programming
    • C#
    • JavaScript
    • Visual Basic.NET
    • ASP.NET

    Lokesh B R

    Expert in:

    • .NET Programming
    • ASP.NET
    • C#
    • Visual Basic.NET

    Éric Moreau

    2

    Articles

    Expert in:

    • .NET Programming
    • Visual Basic.NET
    • C#
    • Visual Basic Classic
    • ASP.NET

    jorge_toriz

    Expert in:

    • ASP.NET
    • .NET Programming
    • C#
    • MS SQL Server
    • MS SQL Server 2005

    Gustav Brock

    9

    Articles

    Expert in:

    • MS Access
    • Query Syntax
    • Databases
    • Visual Basic Classic
    • .NET Programming

    RELATED TOPICS view all topics

    1. ASP.NET
      (122,067)
    2. C#
      (90,373)
    3. Visual Basic.NET
      (91,420)
    4. MS Development
      (48,689)
    5. Editors IDEs
      (25,092)
    6. Visual Basic Classic
      (162,808)
    7. MS SQL Server
      (154,632)
    8. MS SQL Server 2005
      (71,585)
    9. Programming
      (51,613)
    10. JavaScript
      (117,313)