我对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/