字节和宽流Byte and Wide Streams

字节流将文件视作一个字节序列。A byte stream treats a file as a sequence of bytes. 在程序中,字节流是相同的字节序列。Within the program, the stream is the identical sequence of bytes.

相比之下,宽流将文件视作一个通用的多字节字符序列,可以具有广泛的编码规则。By contrast, a wide stream treats a file as a sequence of generalized multibyte characters, which can have a broad range of encoding rules. (文本和二进制文件仍然如前所述进行读取和写入。)在程序中,宽流看起来像相应的宽字符序列。(Text and binary files are still read and written as previously described.) Within the program, the stream looks like the corresponding sequence of wide characters. 两种表示形式之间的转换发生在标准 C 库中。Conversions between the two representations occur within the Standard C Library. 原则上,转换规则可以通过调用更改类别 LC_CTYPEsetlocale 进行更改。The conversion rules can, in principle, be altered by a call to setlocale that alters the category LC_CTYPE. 每个宽流在其变为面向宽度时确定其转换规则,并且即使类别 LC_CTYPE 随后发生更改也会保留这些规则。Each wide stream determines its conversion rules at the time it becomes wide oriented, and retains these rules even if the category LC_CTYPE subsequently changes.

在宽流中进行定位会受到与文本流相同的限制。Positioning within a wide stream suffers the same limitations as for text steams. 此外,文件位置指示器可能必须处理依赖于状态的编码。Moreover, the file-position indicator may well have to deal with a state-dependent encoding. 通常,它包含流内的字节偏移量和 mbstate_t 类型的对象。Typically, it includes both a byte offset within the stream and an object of type mbstate_t. 因此,获取宽流中的文件位置的唯一可靠方法是调用 fgetpos,并且还原以这种方法获得的位置的唯一可靠方法是调用 fsetposThus, the only reliable way to obtain a file position within a wide stream is by calling fgetpos, and the only reliable way to restore a position obtained this way is by calling fsetpos.

另请参阅See Also

文件和流 Files and Streams
setlocale、_wsetlocalesetlocale, _wsetlocale