關於FILE、標準I/O

C-style file input/output - cppreference.com
http://en.cppreference.com/w/cpp/io/cc++

C-style file input/output

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. 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.



Narrow and wide orientation   應該是關於寬字符和窄字符的訪問

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.

Binary and text modes  關於二進制方式讀取和文本方式讀取

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:

在下列狀況下,以前寫入一個流中的數據會和從一個文本流中讀入的數據保證相等。即在其餘狀況下,可能寫入的數據會被系統轉換。

  • the data consist only of printing characters and the control characters \t and \n (in particular, on Windows OS, the character '\0x1A' terminates input)
    特別注意:絕對不能有\r\n,由於\r\n會寫入時被轉換成\n.這也是空白字符規定只能有\t和\n的緣由。
  • no \n is immediately preceded by a space character (space characters that are written out immediately before a \n may disappear when read)
    注:第二個條件說的是,沒有一個\n前面直接跟一個空字符。(由於在輸出時一個在\n前的空字符可能會在讀取時消失。)
  • the last character is \n.注:難道若是本身不加換行符時,系統會幫着加一個,從而致使輸入和輸出不一致。

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實現不區分文本流和二進制流。

Functions

Defined in header <cstdio>

相關文章
相關標籤/搜索