C-style file input/output - cppreference.com
http://en.cppreference.com/w/cpp/io/cc++
The C I/O subset of the C++ standard library implements C-style stream input/output operations. The <cstdio>
header provides generic file operation support and supplies functions with narrow and multibyte character(窄字符或多字节字符) input/output capabilities, and the <cwchar>
header provides functions with wide character input/output capabilities. windows
C streams are objects of type std::FILE that can only be accessed and manipulated through pointers of type std::FILE* (Note:FILE通常只能经过FILE*访问和操做?)while it may be possible to create a local object of type std::FILE by dereferencing and copying a valid std::FILE*, using the address of such copy in the I/O functions is undefined behavior(note:尽管可能经过解引用建立一个本地FILE对象,从而复制一个有效的FILE*,可是在I/O函数中使用这个副本是未定义的行为). Each C stream is associated with an external physical device (file, standard input stream, printer, serial port, etc note:这里将文件、标准输入流、打印机、串口也看成一种外部的物理设备). 多线程
note: C streams是只能用std::FILE的指针类型访问的一类对象。 流是和文件联系在一块儿的一种概念。 app
C streams can be used for both unformatted and formatted input and output. They are locale-sensitive and may perform wide/multibyte conversions as necessary. Unlike C++ streams, where each stream is associated with its own locale, all C streams access the same locale object: the one most recently installed with std::setlocale.(note:c++流每一个流都有本身的本地化设置,而全部的C流都共享同一个本地化对象,要实现对每一个流指定本身特定的locale设置时,必须在使用前本身设定该变量。若是是多线程操做呢?是否须要考虑同步的问题????下面有提到相关内容。) less
Besides the system-specific information necessary to access the device (e.g. a POSIX file descriptor), each C stream object holds the following: ide
1) Character width: unset, narrow or wide 函数
2) Buffering state: unbuffered, line-buffered, fully buffered. ui
3) The buffer, which may be replaced by an external, user-provided buffer. this
4) I/O mode: input, output, or update (both input and output). spa
5) Binary/text mode indicator.
6) End-of-file status indicator.
7) Error status indicator.
8) File position indicator (an object of type std::fpos_t
), which, for wide character streams, includes the parse state (an object of type std::mbstate_t).
9) (C++17) Reentrant lock (重入锁)used to prevent data races when multiple threads read, write, position(定位), or query the position of a stream.
A newly opened stream has no orientation. The first call to std::fwide or to any I/O function establishes the orientation: wide I/O function makes the stream wide-oriented, narrow I/O function makes the stream narrow-oriented. Once set, orientation can only be changed with std::freopen. Narrow I/O functions cannot be called on a wide-oriented stream, wide I/O functions cannot be called on a narrow-oriented stream. Wide I/O functions convert between wide and multibyte characters as if by calling std::mbrtowc and std::wcrtomb. Unlike the multibyte character strings that are valid in a program, multibyte characters in the file may contain embedded nulls and do not have to begin or end in the initial shift state. (TODO,没看懂,之后再说!!!!骗本身?)
POSIX requires that the LC_CTYPE facet of the currently installed C locale is stored within the stream object the moment its orientation becomes wide, and is used for all future I/O on this stream until the orientation is changed, regardless of any subsequent calls to std::setlocale.
A text stream is an ordered sequence of characters composed into lines (zero or more characters plus a terminating '\n'). Whether the last line requires a terminating '\n' is implementation-defined. Characters may have to be added, altered, or deleted on input and output to conform to the conventions for representing text in the OS (in particular, C streams on Windows OS convert \n
to \r\n
on output, and convert \r\n
to \n
on input。注:在windows内部认为换行就应该是\n,但在输出时为了实现回车换行的效果,将内部的\n转换成\r\n.)
注:在输出,即printf或cout输出时,将\n转换成\r\n即回车换行。而对于输入时,如何输入\r\n呢?
Data read in (note:做Data的定语)from a text stream is guaranteed to compare equal to the data that were earlier written out to that stream only if all of the following is true:
在下列状况下,以前写入一个流中的数据会和从一个文本流中读入的数据保证相等。即在其余状况下,可能写入的数据会被系统转换。
\t
and \n
(in particular, on Windows OS, the character '\0x1A'
terminates input) A binary stream is an ordered sequence of characters that can transparently record internal data. Data read in from a binary stream always equals to the data that were earlier written out to that stream. (这是二进制读取相对于文本方式读取的一大特色。)Implementations are only allowed to append a number of null characters to the end of the stream. A wide binary stream doesn't need to end in the initial shift state. (???什么意思??)
POSIX implementations do not distinguish between text and binary streams (there is no special mapping for \n or any other characters)
POSIX实现不区分文本流和二进制流。
Defined in header <cstdio>