locked
Strange behavior when writing byte 0x0D to file RRS feed

  • Question

  • Hi,

    I have been bothered by this issue for whole day. I am trying to write bytes to a file, but the byte 0x0D is always appended with a 0x0A. The strange thing is that I already open the file in binary mode, but this strange behavior still exists. the following is a simple test code I wrote

    //-------------

    #include "stdio.h"

    int main(int argc, char* argv[])
    {
    FILE *ptr = fopen("Test.out", "wb");
    fprintf(ptr, "%c", 0x0d);
    fprintf(ptr, "%c", 0x0d);
    fprintf(ptr, "%c", 0x0d);

    fclose(ptr);
    return 0;
    }
    // ------------


    No matter how I opened the file (in Text mode "w" or in Binary mode "wb" or "w+b" or "wb+" or "a+b"....), the output is always

    0x0D 0x0A 0x0D 0x0A 0x0D 0x0A

    If I put 4 0x00 at the begining, the no more 0x0A is appended.

    I tried on two different machine with VC++6 and VS2005. Same behavior. Any idea what went wrong?
    I also tried using fputc/fwrite and fstream, same behavior.

    Thanks

    Monday, April 26, 2010 10:25 AM

Answers

  • I use UltraEdit which allows you to switch to Hex mode to see the actual bytes. So I think it is not editor.

    Sounds like UltraEdit is converting your binary file to text before you get a chance to look at the bytes. See if it has an "open binary file" mode. If this were Unix, I'd suggest using "od" at the command line. Anyone know if Windows has a similar file dumper program?
    Doug Harrison (Visual C++ MVP)
    • Proposed as answer by Yi Feng Li Friday, April 30, 2010 4:31 AM
    • Marked as answer by Yi Feng Li Wednesday, May 5, 2010 1:55 AM
    Tuesday, April 27, 2010 5:58 AM

All replies

  • heliboy wrote:
    > I have been bothered by this issue for whole day. I am trying to write bytes to a file, but the byte 0x0D is always appended with
    > a 0x0A. The strange thing is that I already open the file in binary mode, but this strange behavior still exists. the following
    > is a simple test code I wrote 
    >
    > //-------------
    >
    > #include "stdio.h"
    >
    > int main(int argc, char* argv[])
    > {
    > FILE *ptr = fopen("Test.out", "wb");
    > fprintf(ptr, "%c", 0x0d);
    > fprintf(ptr, "%c", 0x0d);
    > fprintf(ptr, "%c", 0x0d);
    >
    > fclose(ptr);
    > return 0;
    > }
     
    Works for me (in VC8 aka VS2005). Your code, exactly as shown, produces a file containing 3 bytes 0x0D, and nothing else.
     
    Are you sure you are actually looking at the file created by the most recent run of your program? Delete it, run the program again, see if it reappears. Perhaps you are mistaken as to where the current working directory is for your program.
    --
    Igor Tandetnik
    Monday, April 26, 2010 11:44 AM
  • Hi, thanks for testing for me. It is good to hear that on your machine it works as expected.

     

    I am pretty sure that the file is most recent, no problem there.

     

    I used Turbo C to test, and same behavior! It starts to make me believe that it is the entire development enviroment/OS screwed up. Otherwise why Turbo C behaves the same!

    I believe the File IO kernel is damaged so that different codes generated by different compilers behave the same!

    Monday, April 26, 2010 12:00 PM
  • How are you validating this?

    Certain editors will automatically add the newline character when it finds a lone carriage return.

    So if you opened your file in such an editor this can happen.


    «_Superman_»
    Microsoft MVP (Visual C++)
    Monday, April 26, 2010 5:31 PM
  • Hi,

     

    I use UltraEdit which allows you to switch to Hex mode to see the actual bytes. So I think it is not editor.

     

    I managed to find other computers which produce the same problem. On these computers there is one common thing, they all have both VC++6 and VS2005 installed. I suspect these two cause some kind of conflict.

     

    Is there any way to test this at MS to see if this is the reason and potentially a bug?

     

    Tuesday, April 27, 2010 3:08 AM
  • What is the size of the file after you write 3 0x0D characters?

    Try using fscanf to read the file instead of opening it in the editor.

    Also try fwrite and fread instead of fprintf and fscanf.

     


    «_Superman_»
    Microsoft MVP (Visual C++)
    Tuesday, April 27, 2010 5:24 AM
  • I use UltraEdit which allows you to switch to Hex mode to see the actual bytes. So I think it is not editor.

    Sounds like UltraEdit is converting your binary file to text before you get a chance to look at the bytes. See if it has an "open binary file" mode. If this were Unix, I'd suggest using "od" at the command line. Anyone know if Windows has a similar file dumper program?
    Doug Harrison (Visual C++ MVP)
    • Proposed as answer by Yi Feng Li Friday, April 30, 2010 4:31 AM
    • Marked as answer by Yi Feng Li Wednesday, May 5, 2010 1:55 AM
    Tuesday, April 27, 2010 5:58 AM
  • Hi,

    I tested it too. See source... but, I got 3 bytes long file. 0x0d 0x0d 0x0d.....

    // Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include "stdio.h"

     

    int _tmain(int argc, _TCHAR* argv[])
    {

     FILE *ptr = fopen("C:\\Test.out", "wb");

     fprintf(ptr, "%c", 0x0d);
     fprintf(ptr, "%c", 0x0d);
     fprintf(ptr, "%c", 0x0d);

     fclose(ptr);

     return 0;

    }

     

     

    Tuesday, April 27, 2010 6:48 AM