[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来获取,也可以通过任意重定位函数进行重置。
- 大赛说明
- PAT乙集题库
- 模板
- 1001.害死人的3n+1 c
- 1002.写出这个数 c 02
- 1003.我要通过(20)c03
- 1004.成绩排名 C 04
- 1005.继续(3n+1)猜想 (25) c05
- 1006. 换个格式输出整数 (15) c06
- 1007.素数对猜想 (20) C 07
- 1008. 数组元素循环右移问题 (20) c08
- 1009.说反话(20)c09
- 1010.一元多项式求导 (25) c10
- 1011. A+B和C(15)c11
- 1012.数字分类 C12
- 1013.数素数(20) C 13
- 1014.福尔摩斯的约会(20)c 14
- 1015.德才论c15
- 1016.部分A+B C 16
- 1018.锤子剪刀布c18
- 1023 组个最小数(20) C23
- 1024 科学计数法(20) C24
- 1025.反转链表(25)C25
- 1026.程序运行时间(15)c 26
- 1027. 打印沙漏(20) C 27
- 1028 人口普查(20) C 01
- 1029. 旧键盘(20) C 01
- 1030.完美数列 C 04
- 1031. 查验身份证(15) c05
- 1032. 挖掘机技术哪家强(20) c06
- 1033. 旧键盘打字(20) C 07
- 1034. 有理数四则运算 08
- 1035.插入和归并 C 09
- 1036.跟奥巴马一起编程(15)C18
- 1037.在霍格沃茨找零钱C 11
- 1039.到底买不买 C 13
- 1041.考试座位号(15)C03
- 1042。字符统计 C16
- 1044.火星数字 C 15
- 1046. 划拳(15) C 01
- 1047.编程团体赛C18
- 1051.复数乘法(15) c24
- 1054. 求平均值 (20) C
- 1036. 跟奥巴马一起编程(15) c10
- 1055.集体照 C 09
- 1050. 螺旋矩阵
- C语言
- assert.h
- stdio.h
- Operations on files
- File access
- Formatted input/output
- Character input/output
- Direct input/output
- File positioning
- Error-handling
- Macros
- 数据结构
- 算法
- 5月8日测试题目
- 乔栋
- 01申彦栋
- 02方常吉
- 03王永明
- 04刘果静
- 05原晓晓
- 09牛锦江
- 10李泽路
- 11葛建辉
- 12程才耀
- 13王旭昕
- 16田鹏
- 15李新奇
- 24王翡
- 18高捷
- 07耿生年
- 05王进
- 25闫鑫炎
- 5月9日测试题目
- 热身题库
- L1-1
- L1-2
- L1-3
- L1-4
- L1-5
- L1-6
- L1-8
- L1-7
- L2-1
- L2-2
- L2-3
- L2-4
- L3-1
- L3-2
- L3-3