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

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    Top Expert Contributor

    Essential articles and videos from the Experts

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    More valuable questions with Expert answers

    Question: Hello Folks, I've one website. I've multiple tenants (customers). So customer will be having own URLs but website will be single. Do you know, how to do that in ASP.NET? Example: I've website www.example.com and three tenants then URL...

    Answer: In your DNS you configure a wildcard (*) CNAME record. In your table (lets call it accounts) you add a column (let's call it uri). You populate this column with Tenant1,Tenant2 and Tenant3. Now in the following code: (CODE) The...

    Question: When I try to edit a form I get the error. Some background. I recently uninstalled Office 2003 and installed Office 2013. When I try to build the appl, there are 2 warnings regarding a reference to DAO: Cannot find wrapper assembly for...

    Answer: the dao control is missing. to access sql server you could use odbc, dao, or ole db. can you post the code where you connect and access the rdbms? Sara

    Question: 05 July 201 passed to this function and it returns 05 July 0201 is there a way to check if the date is valid my function is Public Function ConvDate(ByVal strInfield As String) As Tuple(Of Boolean, String) 'Format date...

    Answer: your date can be valid! If you don't want the year 201 to be valid, you will need to add something like this after the Parse: (CODE)

    Question: Hello, I am trying to update records displayed in a RadGrid using a detailsview control. The problem I am facing is that once the "Save" / update event happens, the RadGrid does not update unless I refresh the page, which I am trying to avoid. ...

    Answer: I'm not familiar with that plugin, but my guess would be this is a binding issue, or, perhaps you need to enable View State ?

    201601-LO-Qu-076

    Extend your technology team with the Experts Exchange community.

    — trusted by —

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

    it_saige

    1,274

    Solutions

    Expert in:

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

    Fernando Soto

    5,180

    Solutions

    Expert in:

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

    Karrtik Iyer

    293

    Solutions

    Expert in:

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

    Guy Hengel [angelIII / a3]

    11

    Articles

    37,333

    Solutions

    Expert in:

    • MS SQL Server
    • MS SQL Server 2005
    • Databases-Other
    • MS SQL Server 2008
    • MySQL Server

    käµfm³d   👽

    10

    Articles

    7,570

    Solutions

    Expert in:

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

    Éric Moreau

    2

    Articles

    10,610

    Solutions

    Expert in:

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

    sarabande

    1,396

    Solutions

    Expert in:

    • C++
    • C
    • Programming-Other
    • Visual C++.NET
    • .NET Programming

    Jacques Bourgeois (James Burger)

    2

    Articles

    2,171

    Solutions

    Expert in:

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

    funwithdotnet

    167

    Solutions

    Expert in:

    • .NET Programming
    • ASP.NET
    • Visual Basic.NET

    RELATED TOPICS view all topics

    1. ASP.NET
      (122,582)
    2. C#
      (90,571)
    3. Visual Basic.NET
      (91,311)
    4. MS Development-Other
      (47,891)
    5. Editors IDEs
      (24,919)
    6. Visual Basic Classic
      (163,326)
    7. MS SQL Server
      (155,368)
    8. MS SQL Server 2005
      (71,261)
    9. Programming-Other
      (51,584)
    10. JavaScript
      (118,147)