輸入輸出資料流類iostream常用函數解析

來源:互聯網
上載者:User

標籤:

原創作品,轉載請註明出處:http://www.cnblogs.com/shrimp-can/p/5657192.html 

一.成員類型

1. ios::fmtflags

格式標誌,常用來設定輸出的格式,用於函數flags、setf、unsetf作為其參數或傳回型別。

field member constant effect when set
independent flags boolalpha read/write bool elements as alphabetic strings (true and false).
showbase write integral values preceded by their corresponding numeric base prefix.
showpoint write floating-point values including always the decimal point.
showpos write non-negative numerical values preceded by a plus sign (+).
skipws skip leading whitespaces on certain input operations.
unitbuf flush output after each inserting operation.
uppercase write uppercase letters replacing lowercase letters in certain insertion operations.
numerical base 
(basefield)
dec read/write integral values using decimal base format.
hex read/write integral values using hexadecimal base format.
oct read/write integral values using octal base format.
float format 
(floatfield)
fixed write floating point values in fixed-point notation.
scientific write floating-point values in scientific notation.
adjustment 
(adjustfield)
internal the output is padded to the field width by inserting fill characters at a specified internal point.
left the output is padded to the field width appending fill characters at the end.
right the output is padded to the field width by inserting fill characters at the beginning.

 2. ios_base::iostate

flag value indicates
eofbit End-Of-File reached while performing an extracting operation on an input stream.
failbit The last input operation failed because of an error related to the internal logic of the operation itself.
badbit Error due to the failure of an input/output operation on the stream buffer.
goodbit No error. Represents the absence of all the above (the value zero).

3. 其他:sentry、event、event_callback、failure、Init、openmode、seekdir

 

二.從輸入資料流類istream繼承的函數

1. gcount:streamsize gcount() const;

返回從最後次非格式化輸入操作中提取的讀取的字元個數

  char str[20];  std::cout << "Please, enter a word: ";  std::cin.getline(str,20);  std::cout << std::cin.gcount() << " characters read: " << str << ‘\n‘;
輸出為:
Please, enter a word: simplify9 characters read: simplify

2. get:int get();讀取一個字元,通常用來排除分行符號的影響

           istream& get (char& c);讀取一個字元到c中

           istream& get (char* s, streamsize n);讀取一個字串,直到遇到分行符號或者讀取了n-1個字元,會自動在末尾添加字元‘\0‘

           istream& get (char* s, streamsize n, char delim);讀取一個字串,直到遇到字元delim或者讀取了n-1個字元,會自動在末尾添加字元‘\0‘

          分行符號會留在輸入隊列中

3. getline:istream& getline (char* s, streamsize n );讀取一個字串,直到遇到分行符號或者讀取了n個字元(分行符號也會讀取),並將分行符號替換為‘\0‘

                istream& getline (char* s, streamsize n, char delim );讀取一個字串,直到遇到字元delim或者讀取了n個字元,並將分行符號替換為‘\0‘

3. ignore:istream& ignore (streamsize n = 1, int delim = EOF);

從輸入隊列中提取字元,並忽略它們,直到提取完前面n個字元或者遇到字元delim為止(delim也提取並忽略)。完成後輸入隊列中的第一個字元為第n+1個字元或者delim後面個字元

  char first, last;  std::cout << "Please, enter your first name followed by your surname: ";  first = std::cin.get();     // get one character  std::cin.ignore(256,‘ ‘);   // ignore until space  last = std::cin.get();      // get one character  std::cout << "Your initials are " << first << last << ‘\n‘;

輸出為:

Please, enter your first name followed by your surname: John SmithYour initials are JS

4. 其他:peek、read、readsome、putback、unget

5. tellg、seekg

6. sync:int sync();

輸入緩衝區同步

 

三、從輸出資料流中繼承的函數

1. put:ostream& put (char c);

輸出字元c

2. 其他:write、tellp、seekp、flush

 

四、保護成員函數(C++11)

=操作、swap

 

五、從ios類繼承來的函數

1. ios::good:bool good() const;

如果錯誤狀態標識(eofbit、failbit、badbit)被設定了返回false,如果沒有返回ture

2. ios::eof:bool eof() const;

如果eofbit被設定了返回ture,說明到達了輸入末尾了

3. fail:bool fail() const;

如果failbit或者badbit被設定了返回true

4. bad:bool bad() const;

如果badbit被設定了返回true

iostate value
(member constants)
indicates functions to check state flags
good() eof() fail() bad() rdstate()
goodbit No errors (zero value iostate) true false false false goodbit
eofbit End-of-File reached on input operation false true false false eofbit
failbit Logical error on i/o operation false false true false failbit
badbit Read/writing error on i/o operation false false true true badbit

 5. ios::!操作

如果failbit或者badbit被設定了返回true

6. ios::clear:void clear (iostate state = goodbit);

為錯誤狀態標識設定新的值

7. ios::fill:char fill() const;返回填充字元

                char fill (char fillch);用字元fillch進行填充,返回填充字元

填充輸出的空閑空間

  char prev;  std::cout.width (10);  std::cout << 40 << ‘\n‘;  prev = std::cout.fill (‘x‘);  std::cout.width (10);  std::cout << 40 << ‘\n‘;  std::cout.fill(prev);

輸出:

40xxxxxxxx40

8. 其他:rdstate、setstate、copyfmt、exceptions、imbue、tie、rdbuf、narrow、widen

 

六、從ios_base類中繼承的函數

1. flags:fmtflags flags() const;返回當前格式

             fmtflags flags (fmtflags fmtfl);設定新的格式,返回之前的格式

  std::cout.flags ( std::ios::right | std::ios::hex | std::ios::showbase );  std::cout.width (10);  std::cout << 100 << ‘\n‘;

輸出:      0x64

2. setf:fmtflags setf (fmtflags fmtfl);

            fmtflags setf (fmtflags fmtfl, fmtflags mask);

設定格式,返回設定之前的格式

  std::cout.setf ( std::ios::hex, std::ios::basefield );  // set hex as the basefield  std::cout.setf ( std::ios::showbase );                  // activate showbase  std::cout << 100 << ‘\n‘;  std::cout.unsetf ( std::ios::showbase );                // deactivate showbase  std::cout << 100 << ‘\n‘;

輸出:

0x6464

3. unset:void unsetf (fmtflags mask);

清空mask選擇的格式

4. precision:streamsize precision() const;返回當前的浮點精度(即小數點位元)

                   streamsize precision (streamsize prec);設定浮點精度,返回之前的浮點精度

5. width:streamsize width() const;返回當前域寬度

              streamsize width (streamsize wide);設定域寬度,返回之前的域寬度

6. 其他:imbue、getloc、xalloc、iword、pword、register_callback、sync_with_stdio

參考:http://www.cplusplus.com/reference/istream/iostream/

 

輸入輸出資料流類iostream常用函數解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.