二进制文件和文本文件在存储方面有何区别?

发布于 2024-12-10 20:58:26 字数 279 浏览 0 评论 0原文

我想问一些关于位和字节的问题,因为我很困惑。

  1. 例如,假设我有一个 short int x = 345;。我知道 Short 需要 16 位,所以当我在文件中写入 Short 时,它会被写入 char '3' 、 '4' 、'5' ,每个包含 1 个字节,因此总共 24 位。 有没有办法将文件中的数字(短整数)写为 16 位的短整数?

  2. 我的想法也是对的吗?如果文件是二进制文件或以字节为单位的文本文件,写入文件会有什么区别?

I want to ask few questions about bits and bytes as I am very confused.

  1. For example, suppose I have a short int x = 345;. I know that short take 16 bits, so when I write the short in a file it is written as char '3' , '4' ,'5' each containing 1 bytes so total of 24 bits.
    Is there a way to write the number (short integer) in file as short integer taking 16 bits?

  2. Also am I right in my concept? What will be the difference in writing to my file if the file is binary or text in terms of bytes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2024-12-17 20:58:26

是的,有办法。

uint16_t sh = 345;
fwrite(&sh, sizeof(sh), 1, fp);

在您提到的情况下,345 被写为文本(例如 ASCII,如果您使用的是 ASCII)。在我发布的示例中,sh 的二进制表示形式写入文件中,并且只需要 2 个字节。

如果文件是,写入我的文件会有什么区别
以字节为单位的二进制还是文本?

  1. 文本写入(fprintf

    <前><代码>0000000: 00110011 00110100 00110101
    3 4 5

  2. 二进制写入 (fwrite)

    <前><代码>0000000: 01011001 00000001
    #小端。读作:00000001 01011001 = 345

如果互操作性是一个问题(即您想将文件发送到另一台机器),文本格式是一个更好的选择,因为它是可移植的。

Yes, there is a way.

uint16_t sh = 345;
fwrite(&sh, sizeof(sh), 1, fp);

In the case you mentioned 345 is written as text (for example ASCII if that's what you use). In the example I posted, the binary representation of sh is written in the file and it will take only 2 bytes.

What will be the difference in writing to my file if the file is
binary or text in terms of bytes?

  1. Text write (fprintf)

    0000000: 00110011 00110100 00110101
                3        4        5
    
  2. Binary write (fwrite)

    0000000: 01011001 00000001
    #Little endian. Read as: 00000001 01011001 = 345
    

If interoperabillity is an issue (i.e. you want to send the file to another machine) the text format is a superior choice as it's portable.

灯下孤影 2024-12-17 20:58:26

如果将值写成字符串,则三位数字至少占用三个字节;通常还会有换行符或空格来标记值的结尾。

是的,您可以将该值写为 2 个字节。一种方法是:

fwrite(&x, sizeof(x), 1, fp);

二进制和文本之间的区别在于,您可以在不同类型的机器之间传输文本,几乎完全不受惩罚,并且所有机器都会以相同的方式解释数据。二进制文件只能在具有相同字节序的机器上解释(大字节序或非英特尔与小字节序或英特尔)。在另一类机器上,您必须交换字节顺序才能正确解释二进制数据。

If you write the value as a string, it will occupy at least three bytes for the three digits; there would often be a newline or space as well to mark the end of the value.

Yes, you can write the value as 2 bytes. One way would be:

fwrite(&x, sizeof(x), 1, fp);

The difference between binary and text is that you can transport the text between different types of machine with almost complete impunity and all the machines will interpret the data the same way. The binary file can only be interpreted on machines that have the same endian-ness (big-endian or non-Intel vs little-endian or Intel). On the other class of machines, you have to swap the order of the bytes to get the binary data interpreted correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文