奇怪的0x0D被添加到我的二进制文件 [英] Strange 0x0D being added to my binary file

查看:236
本文介绍了奇怪的0x0D被添加到我的二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题:

我将16个字符写入二进制文件,然后写入3个整数,但是当我使用某些二进制文件查看器打开文件时,我看到添加了一个额外的字节(等于 0x0D )./p>

这是我的代码:

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);

我得到以下字节值:

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

当我期望的时候:

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

有人知道为什么会发生吗?

解决方案

0A是换行符,0D是回车符.这些通常与文本模式关联.

您是否以二进制模式打开文件? (例如fopen("foo.txt", "wb"))

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?

解决方案

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"))

这篇关于奇怪的0x0D被添加到我的二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆