writing 0x0A to a bin file without having it automatically write 0x0D

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MCondon77@gmail.com

    writing 0x0A to a bin file without having it automatically write 0x0D

    This is probably a common problem, but as a novice programmer I'm
    trying to figure out how to write 0x0A to a BIN file without having it
    automatically write 0x0D. Here's an example of the code:
    tmp_ifp = fopen("tmp_name .bin", "w");
    fprintf(tmp_ifp , "%c", 0x0A);

    When I open the tmp_name.bin file with a hex editor you notice that
    both 0x0D and 0x0A are written. I tried using fwrite, but the same
    thing occurs. Any ideas?

    Thanks....

  • John Devereux

    #2
    Re: writing 0x0A to a bin file without having it automatically write 0x0D

    MCondon77@gmail .com writes:
    [color=blue]
    > This is probably a common problem, but as a novice programmer I'm
    > trying to figure out how to write 0x0A to a BIN file without having it
    > automatically write 0x0D. Here's an example of the code:
    > tmp_ifp = fopen("tmp_name .bin", "w");
    > fprintf(tmp_ifp , "%c", 0x0A);
    >
    > When I open the tmp_name.bin file with a hex editor you notice that
    > both 0x0D and 0x0A are written. I tried using fwrite, but the same
    > thing occurs. Any ideas?[/color]

    You need to open the file in "binary" mode, e.g.

    tmp_ifp = fopen("tmp_name .bin", "wb");


    --

    John Devereux

    Comment

    • Ben Pfaff

      #3
      Re: writing 0x0A to a bin file without having it automatically write 0x0D

      MCondon77@gmail .com writes:
      [color=blue]
      > This is probably a common problem, but as a novice programmer I'm
      > trying to figure out how to write 0x0A to a BIN file without having it
      > automatically write 0x0D. Here's an example of the code:
      > tmp_ifp = fopen("tmp_name .bin", "w");[/color]

      Use binary mode: "wb" instead of "w".
      --
      "When in doubt, treat ``feature'' as a pejorative.
      (Think of a hundred-bladed Swiss army knife.)"
      --Kernighan and Plauger, _Software Tools_

      Comment

      • Richard G. Riley

        #4
        Re: writing 0x0A to a bin file without having it automatically write 0x0D

        On 2006-03-13, MCondon77@gmail .com <MCondon77@gmai l.com> wrote:[color=blue]
        > This is probably a common problem, but as a novice programmer I'm
        > trying to figure out how to write 0x0A to a BIN file without having it
        > automatically write 0x0D. Here's an example of the code:
        > tmp_ifp = fopen("tmp_name .bin", "w");
        > fprintf(tmp_ifp , "%c", 0x0A);
        >
        > When I open the tmp_name.bin file with a hex editor you notice that
        > both 0x0D and 0x0A are written. I tried using fwrite, but the same
        > thing occurs. Any ideas?
        >
        > Thanks....
        >[/color]

        I think these routines can convert 0xa to a system defined
        line terminator : which I guess in your case is 0xD,0xA.

        Check the options which can follow "w" in your fopen : b or t might
        help if memory serves correctly. Since you talk about "bin" file then
        "wb" might be your man.

        Comment

        • Jordan Abel

          #5
          Re: writing 0x0A to a bin file without having it automatically write 0x0D

          On 2006-03-13, MCondon77@gmail .com <MCondon77@gmai l.com> wrote:[color=blue]
          > This is probably a common problem, but as a novice programmer I'm
          > trying to figure out how to write 0x0A to a BIN file without having it
          > automatically write 0x0D. Here's an example of the code:
          > tmp_ifp = fopen("tmp_name .bin", "w");[/color]

          "wb".
          [color=blue]
          > fprintf(tmp_ifp , "%c", 0x0A);
          >
          > When I open the tmp_name.bin file with a hex editor you notice that
          > both 0x0D and 0x0A are written. I tried using fwrite, but the same
          > thing occurs. Any ideas?
          >
          > Thanks....
          >[/color]

          Comment

          • Jordan Abel

            #6
            Re: writing 0x0A to a bin file without having it automatically write 0x0D

            On 2006-03-13, Richard G. Riley <rgrdev@gmail.c om> wrote:[color=blue]
            > On 2006-03-13, MCondon77@gmail .com <MCondon77@gmai l.com> wrote:[color=green]
            >> This is probably a common problem, but as a novice programmer I'm
            >> trying to figure out how to write 0x0A to a BIN file without having it
            >> automatically write 0x0D. Here's an example of the code:
            >> tmp_ifp = fopen("tmp_name .bin", "w");
            >> fprintf(tmp_ifp , "%c", 0x0A);
            >>
            >> When I open the tmp_name.bin file with a hex editor you notice that
            >> both 0x0D and 0x0A are written. I tried using fwrite, but the same
            >> thing occurs. Any ideas?
            >>
            >> Thanks....
            >>[/color]
            >
            > I think these routines can convert 0xa to a system defined
            > line terminator : which I guess in your case is 0xD,0xA.
            >
            > Check the options which can follow "w" in your fopen : b or t might
            > help if memory serves correctly. Since you talk about "bin" file then
            > "wb" might be your man.[/color]

            t is not standard C<OT>, and where it is used, it means text mode</OT>.

            Comment

            • Barry Schwarz

              #7
              Re: writing 0x0A to a bin file without having it automatically write 0x0D

              On 13 Mar 2006 14:03:44 -0800, MCondon77@gmail .com wrote:
              [color=blue]
              >This is probably a common problem, but as a novice programmer I'm
              >trying to figure out how to write 0x0A to a BIN file without having it
              >automaticall y write 0x0D. Here's an example of the code:
              >tmp_ifp = fopen("tmp_name .bin", "w");
              >fprintf(tmp_if p, "%c", 0x0A);
              >
              >When I open the tmp_name.bin file with a hex editor you notice that
              >both 0x0D and 0x0A are written. I tried using fwrite, but the same
              >thing occurs. Any ideas?
              >[/color]

              You opened the file in text mode. Therefore, it is not a binary file,
              regardless of what the three character extension is. If you want to
              process the file in binary mode, open it binary mode.

              For additional insurance, don't use fprintf. Use fwrite.


              Remove del for email

              Comment

              • Richard Tobin

                #8
                Re: writing 0x0A to a bin file without having it automatically write 0x0D

                In article <bdec12p9ealqaa u89sd1gpt8q1tc2 vrqr1@4ax.com>,
                Barry Schwarz <schwarzb@doezl .net> wrote:[color=blue]
                >For additional insurance, don't use fprintf. Use fwrite.[/color]

                Huh? How can these be different?

                -- Richard

                Comment

                • MikeC_EE99

                  #9
                  Re: writing 0x0A to a bin file without having it automatically write 0x0D

                  Thanks....I opened it with "wb" and it worked. I'm surprised that just
                  using "w" wouldn't. Why would you want to act on the data being
                  written to a file in that manner? Doesn't make sense. If someone
                  wanted a new line then they would just put "\n".
                  Thanks everyone else for your help too!

                  Comment

                  • Chris Dollin

                    #10
                    Re: writing 0x0A to a bin file without having it automatically write 0x0D

                    MikeC_EE99 wrote:
                    [color=blue]
                    > Thanks....I opened it with "wb" and it worked. I'm surprised that just
                    > using "w" wouldn't. Why would you want to act on the data being
                    > written to a file in that manner?[/color]

                    So that you write the correct data.
                    [color=blue]
                    > Doesn't make sense.[/color]

                    Does.
                    [color=blue]
                    > If someone wanted a new line then they would just put "\n".[/color]

                    But in a /file/, on some OSs incl Windows, a new line is represented
                    as \r\n, not just \n. So, for /text streams/, the C library converts
                    \r\n on input to \n, and \n on output to \r.

                    If you don't want that to happen, you're not writing a text stream,
                    so you have to tell C that you want a "binary" file.

                    --
                    Chris "sparqling" Dollin
                    "Who do you serve, and who do you trust?"

                    Comment

                    • Richard G. Riley

                      #11
                      Re: writing 0x0A to a bin file without having it automatically write 0x0D

                      On 2006-03-14, MikeC_EE99 <MCondon77@gmai l.com> wrote:[color=blue]
                      > Thanks....I opened it with "wb" and it worked. I'm surprised that just
                      > using "w" wouldn't. Why would you want to act on the data being
                      > written to a file in that manner? Doesn't make sense. If someone
                      > wanted a new line then they would just put "\n".
                      > Thanks everyone else for your help too!
                      >[/color]

                      Different OSs terminate text lines with different combinations."b "
                      tells the routine to put what you want, not what the file "convention "
                      is on that system

                      Comment

                      • Barry Schwarz

                        #12
                        Re: writing 0x0A to a bin file without having it automatically write 0x0D

                        On 14 Mar 2006 14:04:28 GMT, richard@cogsci. ed.ac.uk (Richard Tobin)
                        wrote:
                        [color=blue]
                        >In article <bdec12p9ealqaa u89sd1gpt8q1tc2 vrqr1@4ax.com>,
                        >Barry Schwarz <schwarzb@doezl .net> wrote:[color=green]
                        >>For additional insurance, don't use fprintf. Use fwrite.[/color]
                        >
                        >Huh? How can these be different?
                        >[/color]
                        fprintf is going to "massage" the format string, making the
                        "appropriat e" substitutions for every conversion specification. As a
                        Windows system specific implementation detail, this massaging also
                        appears to include converting \n to \n\r (or is it \r\n). That this
                        happens on text files is obvious. I don't know whether it also
                        happens on binary files but fwrite definitely performs no such
                        massaging. Hence the phrase about insurance.


                        Remove del for email

                        Comment

                        • Richard Tobin

                          #13
                          Re: writing 0x0A to a bin file without having it automatically write 0x0D

                          In article <uird12prelu8ub 6muau5v7kqve5kd br1la@4ax.com>,
                          Barry Schwarz <schwarzb@doezl .net> wrote:
                          [color=blue]
                          >fprintf is going to "massage" the format string, making the
                          >"appropriate " substitutions for every conversion specification. As a
                          >Windows system specific implementation detail, this massaging also
                          >appears to include converting \n to \n\r (or is it \r\n). That this
                          >happens on text files is obvious. I don't know whether it also
                          >happens on binary files but fwrite definitely performs no such
                          >massaging. Hence the phrase about insurance.[/color]

                          I don't have a Windows machine to test it on, but I can see nothing in
                          the standard to justify this behaviour. The conversion of characters
                          is controlled by the stream type, which is in turn controlled by the
                          "b" flag to fopen(), not by the choice of fprintf() or fwrite().

                          -- Richard

                          Comment

                          • Jordan Abel

                            #14
                            Re: writing 0x0A to a bin file without having it automatically write 0x0D

                            On 2006-03-14, Barry Schwarz <schwarzb@doezl .net> wrote:[color=blue]
                            > On 14 Mar 2006 14:04:28 GMT, richard@cogsci. ed.ac.uk (Richard Tobin)
                            > wrote:
                            >[color=green]
                            >>In article <bdec12p9ealqaa u89sd1gpt8q1tc2 vrqr1@4ax.com>,
                            >>Barry Schwarz <schwarzb@doezl .net> wrote:[color=darkred]
                            >>>For additional insurance, don't use fprintf. Use fwrite.[/color]
                            >>
                            >>Huh? How can these be different?
                            >>[/color]
                            > fprintf is going to "massage" the format string, making the
                            > "appropriat e" substitutions for every conversion specification. As a
                            > Windows system specific implementation detail, this massaging also
                            > appears to include converting \n to \n\r (or is it \r\n).[/color]

                            It is not printf that does this. It is putc, which is called [or as if
                            it is called] by every other output function. \n is not a conversion
                            specification.
                            [color=blue]
                            > That this happens on text files is obvious. I don't know whether it
                            > also happens on binary files but fwrite definitely performs no such
                            > massaging.[/color]

                            It is self-evident that it does, on text files. An implementation where
                            the output caused by fwrite is not as if putc were called in a loop is
                            non-conforming.
                            [color=blue]
                            > Hence the phrase about insurance.[/color]

                            Comment

                            • Jordan Abel

                              #15
                              Re: writing 0x0A to a bin file without having it automatically write 0x0D

                              On 2006-03-14, MikeC_EE99 <MCondon77@gmai l.com> wrote:[color=blue]
                              > Thanks....I opened it with "wb" and it worked. I'm surprised that just
                              > using "w" wouldn't. Why would you want to act on the data being
                              > written to a file in that manner?[/color]

                              Compatibility with text files used by other programs on the platform,
                              some of which may not be written in C.
                              [color=blue]
                              > Doesn't make sense. If someone wanted a new line then they would just
                              > put "\n". Thanks everyone else for your help too!
                              >[/color]

                              Comment

                              Working...