Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange 0x0D being added to my binary file

Tags:

c

file

binary

I have this strange problem:

I write 16 chars to a binary file and then I write 3 integers but when I open my file with some binary file viewer, I see an extra byte is added (which equals 0x0D).

Here's my code:

for(i = 0; i < 16; i++)
{
    if(i < strlen(inputStr))
    {
        myCharBuf[0] = inputStr[i];
    }
    else
    {
        myCharBuf[0] = 0;
    }

    fwrite(myCharBuf, sizeof(char), 1, myFile);
}

myIntBuf[0] = inputNumber1;

fwrite(myIntBuf, sizeof(int), 1 ,myFile);

myIntBuf[0] = inputNumber2;

fwrite(myIntBuf, sizeof(int), 1 ,myFile);

myIntBuf[0] = inputNumber3;

fwrite(myIntBuf, sizeof(int), 1 ,myFile);

I get the following byte-values:

61 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0D 0A 00 00 00 05 00 00 00 08 00 00 00

When I expect:

61 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0A 00 00 00 05 00 00 00 08 00 00 00

Does anyone have an idea why it might happen?

like image 281
M.N Avatar asked Apr 04 '11 10:04

M.N


People also ask

What will happen if you try to open a binary file using a text?

2.2.2 Binary Files Rather, they represent the actual content such as image, audio, video, compressed versions of other files, executable files, etc. These files are not human readable. Thus, trying to open a binary file using a text editor will show some garbage values.

Can you read binary files?

Binary files are not human readable and require a special program or hardware processor that knows how to read the data inside the file. Only then can the instructions encoded in the binary content be understood and properly processed.

What is binary file mode?

(1) A mode of operation that deals with non-textual data. When a "binary" parameter is added to a command, it enables every type of data to be transferred or compared rather than just ASCII text. See ASCII and bit. (2) A compiler mode that deals with file I/O.

What are examples of binary files?

Executable files, compiled programs, SAS and SPSS system files, spreadsheets, compressed files, and graphic (image) files are all examples of binary files.


1 Answers

0A is the line feed character and 0D is the carriage return. These are usually associated with text mode.

Have you opened the file in binary mode? (e.g. fopen("foo.txt", "wb"))

like image 141
Jeff Foster Avatar answered Oct 02 '22 16:10

Jeff Foster