控制流Controlling Streams

fopen 返回 FILE 类型的对象的地址。fopen returns the address of an object of type FILE. 您可以将此地址用作多个库函数的 stream 参数以对打开的文件执行各种操作。You use this address as the stream argument to several library functions to perform various operations on an open file. 对于字节流,会像通过调用 fgetc 读取每个字符一样进行所有输入,并且会像通过调用 fputc 写入每个字符一样进行所有输出。For a byte stream, all input takes place as if each character is read by calling fgetc, and all output takes place as if each character is written by calling fputc. 对于宽流,会像通过调用 fgetwc 读取每个字符一样进行所有输入,并且会像通过调用 fputwc 写入每个字符一样进行所有输出。For a wide stream, all input takes place as if each character is read by calling fgetwc, and all output takes place as if each character is written by calling fputwc.

可以通过调用 fclose 关闭文件,之后,FILE 对象的地址将无效。You can close a file by calling fclose, after which the address of the FILE object is invalid.

FILE 对象存储流的状态,包括:A FILE object stores the state of a stream, including:

  • 错误指示器通过遇到读取或写入错误的函数设置非零值。An error indicator set nonzero by a function that encounters a read or write error.

  • 文件尾指示器通过在读取时遇到文件尾的函数设置非零值。An end-of-file indicator set nonzero by a function that encounters the end of the file while reading.

  • 如果文件可以支持定位请求,则文件位置指示器将在流中指定要读取或写入的下一个字节。A file-position indicator specifies the next byte in the stream to read or write, if the file can support positioning requests.

  • 流状态指定流是否将接受读取和/或写入,以及流是未绑定的、面向字节的还是面向宽度的。A stream state specifies whether the stream will accept reads and/or writes and whether the stream is unbound, byte oriented, or wide oriented.

  • 转换状态将记住任何部分汇编的或生成的通用多字节字符的状态,以及文件中字节序列的所有移位状态。A conversion state remembers the state of any partly assembled or generated generalized multibyte character, as well as any shift state for the sequence of bytes in the file).

  • 文件缓冲区指定数组对象的地址和大小,库函数可使用此对象提高对流进行的读取和写入操作的性能。A file buffer specifies the address and size of an array object that library functions can use to improve the performance of read and write operations to the stream.

    不要修改存储在 FILE 对象中或与该对象一起使用的指定文件缓冲区中的任何值。Do not alter any value stored in a FILE object or in a file buffer that you specify for use with that object. 不能复制 FILE 对象,也不能将副本的地址用作库函数的 stream 参数。You cannot copy a FILE object and portably use the address of the copy as a stream argument to a library function.

另请参阅See Also

文件和流Files and Streams