🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ### 完成输入输出操作的C库函数-C library to perform Input/Output operations Input and Output operations can also be performed in C++ using the C Standard Input and Output Library (cstdio, known as stdio.h in the C language). This library uses what are called streams to operate with physical devices such as keyboards, printers, terminals or with any other type of files supported by the system. Streams are an abstraction to interact with these in an uniform way; All streams have similar properties independently of the individual characteristics of the physical media they are associated with. Streams are handled in the cstdio library as pointers to FILE objects. A pointer to a FILE object uniquely identifies a stream, and is used as a parameter in the operations involving that stream. There also exist three standard streams: stdin, stdout and stderr, which are automatically created and opened for all programs using the library. *** 翻译: 在C++中的输入输出操作也可以使用C标准输入输出库。这个库使用“流”这个概念来处理物理设备的输入输出,如键盘,打印机,终端或者是系统支持的其他类型的文件。流是使用同一的方式,用来和这些设备交互的抽象概念。所有的流都有相似属性,这些属性独立于这些物理设备各自所具有的特有属性。 流处理文件对象的方式是使用指针。一个指向文件对象的指针唯一标识一个流,涉及到流的操作中,可以将其作为一个参数。 一共有三个标准输入输出流:stdin,stdout,stderr。这三个标准输入输出流是自动为所有使用该库的程序自动打开的。 ### 流属性-Stream properties Streams have some properties that define which functions can be used on them and how these will treat the data input or output through them. Most of these properties are defined at the moment the stream is associated with a file (opened) using the fopen function: * Read/Write Access Specifies whether the stream has read or write access (or both) to the physical media they are associated with. * Text / Binary Text streams are thought to represent a set of text lines, each one ending with a new-line character. Depending on the environment where the application is run, some character translation may occur with text streams to adapt some special characters to the text file specifications of the environment. A binary stream, on the other hand, is a sequence of characters written or read from the physical media with no translation, having a one-to-one correspondence with the characters read or written to the stream. * Buffer A buffer is a block of memory where data is accumulated before being physically read or written to the associated file or device. Streams can be either fully buffered, line buffered or unbuffered. On fully buffered streams, data is read/written when the buffer is filled, on line buffered streams this happens when a new-line character is encountered, and on unbuffered streams characters are intended to be read/written as soon as possible. * Orientation On opening, streams have no orientation. As soon as an input/output operation is performed on them, they become either byte-oriented or wide-oriented, depending on the operation performed (generally, functions defined in <cstdio> are byte-oriented, while functions in <cwchar> are wide-oriented). See cwchar for more info. *** 翻译:流有很多属性,这些属性规定了哪些函数可以使用它们,以及这些函数如何通过流来处理数据的输入和输出(这里应该是说读写权限)。大部分这些属性是在文件被打开的时候所定义的,我们一般使用fopen函数来打开文件: * 读写权限 当前流是否有读或者写(或者两者皆有)的权限,对当前物理设备。 * 文本/二进制 文本流表示了一串文本行,以换行符结束。文本流中会有某些字符转义,这是为了适应某些特定文本格式下特殊字符的传输。另一方面,一个二进制流是一系列从物理设备中读或者写的字符,并没有发生任何转义,读写过程中字符是一一对应的。 * 缓存 一个缓存是指一块内存区域,该区域存放在物理读写文件或者设备之前的数据。流缓存分为全缓存,行缓存和无缓存。全缓存中数据的读写发生在缓存变满,行缓存流是当换行符键入的时候发生读写,无缓存流是尽可能去读写。 * 面向 在打开过程中,流并没有指向(这里的指向是指面向字节还是面向宽字节)。(计算机网络中有相关概念) ### 标识符-Indicators Streams have certain internal indicators that specify their current state and which affect the behavior of some input and output operations performed on them: * Error indicator This indicator is set when an error has occurred in an operation related to the stream. This indicator can be checked with the ferror function, and can be reset by calling either to clearerr, freopen or rewind. * End-Of-File indicator When set, indicates that the last reading or writing operation performed with the stream reached the End of File. It can be checked with the feof function, and can be reset by calling either to clearerr or freopen or by calling to any repositioning function (rewind, fseek and fsetpos). * Position indicator It is an internal pointer of each stream which points to the next character to be read or written in the next I/O operation. Its value can be obtained by the ftell and fgetpos functions, and can be changed using the repositioning functions rewind, fseek and fsetpos. *** 流中有自己的内部标识符,用来标记当前状态和一些输入输出操作的结果: * 错误标识符 当流操作出错的时候会用到,可以通过函数ferror来进行检测,也可以通过函数clearerr,freopen,rewind来重置。 * 文件结束标识符 当最后读写流的操作到达文件结束时该标识符会被定义,可以通过feof相关函数进行检测,也可以通过clearerr,freopen或者任何重定位函数来进行重置。 * 定位标识符 每个流的内部指针,指向下一个IO操作中的字符。它的值可通过函数ftell,fgetpos来获取,也可以通过任意重定位函数进行重置。