C++流操纵算子(格式控制)
生活随笔
收集整理的這篇文章主要介紹了
C++流操纵算子(格式控制)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?????? 輸入/輸出的數據沒有指定格式,它們都按缺省的格式輸入/輸出。然而,有時需要對數據格式進行控制。這時需利用ios類中定義的格式控制成員函數,通過調用它們來完成格式的設置。
ios類的格式控制函數如下所示:
| long?flags(?)?const | 返回當前的格式標志。 |
| long?flays(long?newflag) | 設置格式標志為newflag,返回舊的格式標志。 |
| long?setf(long?bits)??? | 設置指定的格式標志位,返回舊的格式標志。 |
| long?setf(long?bits,long?field) | 將field指定的格式標志位置為bits,返回舊的格式標志。 |
| ?long?unsetf(long?bits)? | 清除bits指定的格式標志位,返回舊的格式標志。 |
| long?fill(char?c)?? | 設置填充字符,缺省條件下是空格。 |
| ??char?fill(?)?? | 返回當前填充字符。 |
| int?precision(int?val)?? | 設置精確度為val,控制輸出浮點數的有效位,返回舊值。 |
| int?precision(?) | 返回舊的精確度值。 |
| int?width(int?val)?????? | 設置顯示數據的寬度(域寬),返回舊的域寬。 |
| int?width(?)?? | 只返回當前域寬,缺省寬度為0。這時插入操作能按表示數據的最小寬度顯示數據。? |
預定義的操縱算子
????使用成員函數控制格式化輸入輸出時,每個函數調用需要寫一條語句,尤其是它不能用在插入或提取運算符的表達式中,而使用操縱算子,則可以在插入和提取運算符的表達式中控制格式化輸入和輸出。在程序中使用操縱算字必須嵌入頭文件iomanip.h
| dec | 十進制的輸入輸出 |
| ?hex | 十六進制的輸入輸出 |
| ?oct?? | 八進制的輸入輸出 |
| ws?? | 提取空白字符 |
| ?ends?? | 輸出一個nul字符 |
| endl? | ?輸出一個換行字符,同時刷新流 |
| flush | 刷新流 |
| resetiosflags(long) | 請除特定的格式標志位 |
| setiosflags(long)? | 設置特定的格式標志位 |
| setfill(char) | 設置填充字符 |
| setprecision(int) | 設置輸出浮點數的精確度 |
| ?setw(int) | 設置域寬格式變量 |
其它流函數
錯誤處理
????在對一個流對象進行I/O操作時,可能會產生錯誤。當錯誤發生時,錯誤的性質被記錄在ios類的一個數據成員中。
ios類中定義的描述錯誤狀態的常量:
?
| goodbit? | 沒有錯誤,正常狀態 ?eofbit?到達流的結尾??? |
| failbit | I/O操作失敗,清除狀態字后,可以對流繼續進行操作。 |
| badbit | 試圖進行非法操作,清除狀態字后,流可能還可以使用。 |
| hardfail | 致命錯誤,不可恢復的錯誤。 |
ostream類的成員函數
流的其它成員函數可以從流中讀取字符或字符串,對流進行無格式化的輸入?輸出操作,以及直接控制對流的I/O操作。
?
| 返回類型 | ios類的成員 | 描 述 |
| ostream* | tie(ostream*) | ???將當前流與指定的輸出流連接起來。每當需要?讀取當前流時,連接的流會自動刷新。C++流庫已用cin.tie(cout)將輸入流與輸出流連接起來。要取消與輸出流的連接可采用is.tie(0) |
| ostream* | tie(?) | 返回指向連接流的指針 |
?
| 返回類型 | ostream類的成員 | 描 述 |
| ostream& | put(char?ch) | 向流中輸出一個字符ch,不進行任何轉換 |
| ostream& | write(char*,int) | 向流中輸出指定長度的字符串,不進行轉換 |
| ostream&? | flush(?) | 刷新流,輸出所有緩沖的但還未輸出的數據 |
| ostream& | seekp(streampos) | 移動流的當前指針到給定的絕對位置 |
| ostream& | seekp(sereamoff,seek_dir) | 流的當前指針類似與文件的當前指針 |
| streampos | teelp(?) | 返回流的當前指針的絕對位置 |
istream類的成員函數
?
| 返回類型 | istream類的成員 | 描 述 |
| int | get(?) | 讀取并返回一個字符 |
| istream& | get(char&c) | 讀取字符并存入c中 |
| istream& | get(char*ptr,int?len,char?delim='') | 讀取指定的字符到緩沖區中,直到遇到指定的分界符為止,分界符不填入緩沖區。 |
| istream& | getline(char*ptr,int?len,char?delim='') | 與get(char*ptr,int?len,chardelim?='')?類似,但將分界符填入緩沖區。 |
| istream& | ?putback(?) | 將最近讀取的字符放回流中 |
| istream& | read(char*,int) | 讀取規定長度的字符串到緩沖區中 |
| int | peek(?)? | 返回流中下一個字符,但不移動文件指針 |
| istream& | seekg(streampos) | 移動當前指針到一絕對地址 |
| istream&? | seekg(streampos,seek_dir) | 移動當前指針到一相對地址 |
| streampos | tellg(?) | 返回當前指針 |
| istream& | ignore(int?n=1,delim=EOF) | 跳過流中幾個字符,或直到遇到指定的分界符為止 |
關于::符號的探討
仔細查找了一下ios類控制符的源文件,可以追溯到頭文件ios_base.h,其中包含了很多控制流的變量或方法,而這些成員都被包圍在了命名空間std中,代碼如下所示。因此,流控制符是std命名空間中的靜態常量,::則代表了這些常量來自于std這個命名空間。
1 // 27.4.2.1.2 Type ios_base::fmtflags 2 /** 3 * @brief This is a bitmask type. 4 * 5 * @c "_Ios_Fmtflags" is implementation-defined, but it is valid to 6 * perform bitwise operations on these values and expect the Right 7 * Thing to happen. Defined objects of type fmtflags are: 8 * - boolalpha 9 * - dec 10 * - fixed 11 * - hex 12 * - internal 13 * - left 14 * - oct 15 * - right 16 * - scientific 17 * - showbase 18 * - showpoint 19 * - showpos 20 * - skipws 21 * - unitbuf 22 * - uppercase 23 * - adjustfield 24 * - basefield 25 * - floatfield 26 */ 27 typedef _Ios_Fmtflags fmtflags; 28 29 /// Insert/extract @c bool in alphabetic rather than numeric format. 30 static const fmtflags boolalpha = fmtflags(__ios_flags::_S_boolalpha); 31 32 /// Converts integer input or generates integer output in decimal base. 33 static const fmtflags dec = fmtflags(__ios_flags::_S_dec); 34 35 /// Generate floating-point output in fixed-point notation. 36 static const fmtflags fixed = fmtflags(__ios_flags::_S_fixed); 37 38 /// Converts integer input or generates integer output in hexadecimal base. 39 static const fmtflags hex = fmtflags(__ios_flags::_S_hex); 40 41 /// Adds fill characters at a designated internal point in certain 42 /// generated output, or identical to @c right if no such point is 43 /// designated. 44 static const fmtflags internal = fmtflags(__ios_flags::_S_internal); 45 46 /// Adds fill characters on the right (final positions) of certain 47 /// generated output. (I.e., the thing you print is flush left.) 48 static const fmtflags left = fmtflags(__ios_flags::_S_left); 49 50 /// Converts integer input or generates integer output in octal base. 51 static const fmtflags oct = fmtflags(__ios_flags::_S_oct); 52 53 /// Adds fill characters on the left (initial positions) of certain 54 /// generated output. (I.e., the thing you print is flush right.) 55 static const fmtflags right = fmtflags(__ios_flags::_S_right); 56 57 /// Generates floating-point output in scientific notation. 58 static const fmtflags scientific = fmtflags(__ios_flags::_S_scientific); 59 60 /// Generates a prefix indicating the numeric base of generated integer 61 /// output. 62 static const fmtflags showbase = fmtflags(__ios_flags::_S_showbase); 63 64 /// Generates a decimal-point character unconditionally in generated 65 /// floating-point output. 66 static const fmtflags showpoint = fmtflags(__ios_flags::_S_showpoint); 67 68 /// Generates a + sign in non-negative generated numeric output. 69 static const fmtflags showpos = fmtflags(__ios_flags::_S_showpos); 70 71 /// Skips leading white space before certain input operations. 72 static const fmtflags skipws = fmtflags(__ios_flags::_S_skipws); 73 74 /// Flushes output after each output operation. 75 static const fmtflags unitbuf = fmtflags(__ios_flags::_S_unitbuf); 76 77 /// Replaces certain lowercase letters with their uppercase equivalents 78 /// in generated output. 79 static const fmtflags uppercase = fmtflags(__ios_flags::_S_uppercase); 80 81 /// A mask of left|right|internal. Useful for the 2-arg form of @c setf. 82 static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield); 83 84 /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf. 85 static const fmtflags basefield = fmtflags(__ios_flags::_S_basefield); 86 87 /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf. 88 static const fmtflags floatfield = fmtflags(__ios_flags::_S_floatfield);?
?
總結
以上是生活随笔為你收集整理的C++流操纵算子(格式控制)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3 Useful BookmarkLet
- 下一篇: datasnap的线程池