文本和二进制流Text and Binary Streams

文本流包含一个或多个文本行,这些文本行可写入到面向文本的显示器中以供读取。A text stream consists of one or more lines of text that can be written to a text-oriented display so that they can be read. 当从文本流中进行读取时,程序在每行末尾读取一个 NL(换行符)。When reading from a text stream, the program reads an NL (newline) at the end of each line. 在写入文本流时,程序写入一个 NL 以表示行的结尾。When writing to a text stream, the program writes an NL to signal the end of a line. 为了在目标环境中匹配不同的约定来表示文件中的文本,库函数可以更改在程序和文本流之间传输的字符的数量和表示。To match differing conventions among target environments for representing text in files, the library functions can alter the number and representations of characters transmitted between the program and a text stream.

因此,在文本流中定位是受限的。Thus, positioning within a text stream is limited. 可通过调用 fgetposftell 获取当前文件位置指示符。You can obtain the current file-position indicator by calling fgetpos or ftell. 可以将文本流定位到通过这种方式获取的位置,或者通过调用 fsetposfseek,定位到文本流的开头或结尾。You can position a text stream at a position obtained this way, or at the beginning or end of the stream, by calling fsetpos or fseek. 可能无法很好地支持任何其他位置更改。Any other change of position might well be not supported.

为获得最大的可移植,程序不应写入:For maximum portability, the program should not write:

  • 空文件。Empty files.

  • 行尾的空白字符。Space characters at the end of a line.

  • 部分行(通过在文件尾省略 NL)。Partial lines (by omitting the NL at the end of a file).

  • 除可打印字符、NL 和 HT(水平制表符)之外的字符。characters other than the printable characters, NL, and HT (horizontal tab).

    如果您遵循这些规则,则您从文本流读取的字符序列(作为字节或多字节字符)将与您创建文件时写入到文本流的字符序列匹配。If you follow these rules, the sequence of characters you read from a text stream (either as byte or multibyte characters) will match the sequence of characters you wrote to the text stream when you created the file. 否则,如果您创建的文件在关闭时是空的,则库函数可以删除该文件。Otherwise, the library functions can remove a file you create if the file is empty when you close it. 或者库函数也可更改或删除您写入到该文件的字符。Or they can alter or delete characters you write to the file.

    二进制流包含一个或多个字节的任意信息。A binary stream consists of one or more bytes of arbitrary information. 您可将存储在任意对象中的值写入到一个(面向字节的)二进制流,并读取您在编写二进制流时存储在该对象中的完全相同的内容。You can write the value stored in an arbitrary object to a (byte-oriented) binary stream and read exactly what was stored in the object when you wrote it. 库函数不会更改您在程序和二进制流之间传输的字节。The library functions do not alter the bytes you transmit between the program and a binary stream. 然而,库函数可以将任意数量的 null 字节追加到您使用二进制流编写的文件。They can, however, append an arbitrary number of null bytes to the file that you write with a binary stream. 程序必须在任何二进制流的末尾处理这些附加的 null 字节。The program must deal with these additional null bytes at the end of any binary stream.

    因此,二进制流内的定位可以明确地进行定义,但相对于流的末尾的定位除外。Thus, positioning within a binary stream is well defined, except for positioning relative to the end of the stream. 与文本流相似,可获取和更改当前的文件定位指示符。You can obtain and alter the current file-position indicator the same as for a text stream. 此外,由 ftellfseek 使用的偏移量从流的开头(字节 0)计算字节数,因此有关这些偏移量的整数算法将产生可预知的结果。Moreover, the offsets used by ftell and fseek count bytes from the beginning of the stream (which is byte zero), so integer arithmetic on these offsets yields predictable results.

    字节流将文件视作一个字节序列。A byte stream treats a file as a sequence of bytes. 在程序内部,除了上面所述的可能的更改之外,流类似于相同的字节序列。Within the program, the stream looks like the same sequence of bytes, except for the possible alterations described above.

另请参阅See Also

文件和流Files and Streams