C-style file input/output - cppreference.com
http://en.cppreference.com/w/cpp/io/c
C-style I/O
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.
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的指针类型访问的一类对象。 流是和文件联系在一起的一种概念。
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设置时,必须在使用前自己设定该变量。如果是多线程操作呢?是否需要考虑同步的问题????下面有提到相关内容。)
Besides the system-specific information necessary to access the device (e.g. a POSIX file descriptor), each C stream object holds the following:
1) Character width: unset, narrow or wide
2) Buffering state: unbuffered, line-buffered, fully buffered.
3) The buffer, which may be replaced by an external, user-provided buffer.
4) I/O mode: input, output, or update (both input and output).
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.
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)
Data read in 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:
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)
Defined in header <cstdio>