cin.getline用法說明 - from C++ reference



std::istream::getline

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
Get line

Extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character).

此函數從流類對象中抽取字符作爲非格式化的輸入以c-string類型存儲到 s 中,直到抽取到 delimiting character(作爲界限的字符 '\n')或者寫入到s中的字符的數量(包括作爲字符串結尾的'\0'字符)已經達到n 爲止。

 The delimiting character is the newline character ('\n') for the first form, and delim for the second: when found in the input sequence, it is extracted from the input sequence, but discarded and not written to s.

所謂的delimiting character在默認情況下就是newline character('\n'),在函數帶參(delim)的情況下以delim作爲delimiting character, 具體情況是,當delimiting character出現在輸入字符串中時,它會被從輸入序列中提取出來,但是會被捨棄,不會被寫入到s中。


 The function will also stop extracting characters if the end-of-file is reached. If this is reached prematurely (before either writing n characters or finding delim), the function sets the eofbit flag.

如果文件或者輸入的字符串達到了文件結束符(end-of-file),該函數也會停止。如果這種情況過早出現(在寫入s的字符數量達到n或者找到delim之前),該函數會在內置標誌字中設置eofbit的標誌。

 The failbit flag is set if the function extracts no characters, or if the delimiting character is not found once (n-1) characters have already been written to s. Note that if the character that follows those (n-1) characters in the input sequence is precisely the delimiting character, it is also extracted and the failbit flag is not set (the extracted sequence was exactly n characters long).

如果函數沒有提取到任何字符或者delimiting character在s中已經寫入(n - 1)個字符的時候仍然沒有出現,那麼函數將會在內置標誌字中設置failbit標誌。需要注意的是,如果輸入序列中的第n 個字符恰好是delimiting character,那麼它仍然會被提取但是failbit的標誌不會被設置(被輸入的字符串的長度恰好是n)。

 A null character ('\0') is automatically appended to the written sequence if n is greater than zero, even if an empty string is extracted.

系統會在寫入的字符串的末尾追加字符null character('\0')只要n>0,即便是空字符串(沒有提取到任何字符)。

 Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true). Then (if good), it extracts characters from its associated stream buffer object as if calling its member functions sbumpc or sgetc, and finally destroys the sentry object before returning.

函數在處理輸入字符串(input sequence)時會首先創建一個sentry對象(並把 noskipws 設置爲 true)。然後(如果創建成功),它會從與它相關聯的stream buffer 對象中抽取字符,就像調用它的成員函數sbumpc 或者 sgetc,然後最終在函數返回之前把該 sentry 對象註銷。

 The number of characters successfully read and stored by this function can be accessed by calling member gcount.

本函數成功讀取和存儲的字符數量可以通過調用 gcount 獲得。

 This function is overloaded for string objects in header <string>: See getline(string).

本函數在<string>庫中有以 string 對象爲參數的函數重載。參見 getline(string)。


Parameters (函數參數)

s
Pointer to an array of characters where extracted characters are stored as a c-string.
用來存儲 c-string 對象的(字符串)的數組的指針(即數組名)
n
Maximum number of characters to write to s (including the terminating null character).
能夠寫入s的最大字符數(包括串尾符 '\0')
 If the function stops reading because this limit is reached without finding the delimiting character, the failbit internal flag is set.
streamsize is a signed integral type.
如果函數因爲已經讀入了n個字符卻仍未發現結束標記(delimiting character),那麼函數將在內置標誌字中設置failbit標誌。
delim
Explicit delimiting character: The operation of extracting successive characters stops as soon as the next character to extract compares equal to this.
delim即delimiting character,一旦下一個字符與delim相同,函數就停止讀取字符。

Return Value (返回值)

The istream object (*this).

Errors are signaled by modifying the internal state flags:

錯誤通過修改內置標誌字來標明。

flag error
eofbit

The function stopped extracting characters because the input sequence has no more characters available (end-of-file reached).

函數因爲無法再提取更多的字符而停止(達到文件結束標誌 end-of-file)

failbit

Either the delimiting character was not found or no characters were extracted at all (because the end-of-file was before the first character or because the construction of sentry failed).

沒有發現結束符(delimiting character),或者沒有提取到任何字符(因爲文件結束符end-of-file出現在需要讀入的字符串之前或者sentry的創建失敗)。

badbit

Error on stream (such as when this function catches an exception thrown by an internal operation).
When set, the integrity of the stream may have been affected.

流錯誤(比如說函數捕捉到了內部操作拋出的異常)。如果該標誌被設置,流的完整性可能受到了影響。

Multiple flags may be set by a single operation. 同一個操作可能會設置多個標誌。

If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.

如果函數設置了一個用成員異常註冊的內部標誌字,該函數會拋出一個member type failure的異常。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章