c - C的流中字符的含义

标签 c language-lawyer c99 binary-data

我对C的流抽象中字符的含义的理解似乎有一个盲点;我就是无法将图片拼接在一起。
字符相对于二进制流的含义是什么?

从 7.19.7.1p2 ...

If the end-of-file indicator for the input stream pointed to by stream is not set and a next character is present, the fgetc function obtains that character as an unsigned char converted to an int and advances the associated file position indicator for the stream (if defined).

...

假设我在字符需要 16 位的机器上写了一个文件,然后我开始在字符适合 7 位的机器上读取。那么每次调用 fgetc 时我实际读到的是什么?它是 16 位字符的一部分(即,我一次读取 7 位)还是 16 位字符被“压缩”为 7 位表示并丢失了信息?

最佳答案

来自规范:

3.7.1
1 character
single-byte character
〈C〉 bit representation that fits in a byte

和:

3.6
1 byte
addressable unit of data storage large enough to hold any member of the basic character set of the execution environment
NOTE 1 It is possible to express the address of each individual byte of an object uniquely.
NOTE 2 A byte is composed of a contiguous sequence of bits, the number of which is implementation- defined. The least significant bit is called the low-order bit; the most significant bit is called the high-order bit.

因此在您的书写机器上,char 可能是 16 位类型。在您的阅读机上,char 可能是 8 位类型。 C 要求 char 至少是 8 位类型:

5.2.4.2.1 Sizes of integer types
...
— number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8

因此,在您的阅读机器上,您需要调用两次 fgetc 来读取您在原始机器上写入的 16 位字符的每一半。

关于c - C的流中字符的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54496075/

相关文章:

c - 编译器如何在编译时不知道大小的情况下分配内存?

c++ - C99 和 C++ 的内联函数的兼容定义

c - C 文件如何包含在 freeSwitch(一种调用框架)中?

c - 如何在不使用 c 中的已知函数的情况下舍入到最接近的整数?

c - 运行从代码生成的二进制文件是否具有 "constraint violation"实际上未定义的行为?

c++ - 返回对 InputIterator 内部状态的引用是否合法?

c - API设计——分配输出?

c - 设置线程ID

C++ 默认构造函数与具有非平凡默认构造函数的变体成员 union

更改控制台窗口中的现有文本