Input and output stream class iostream common function parsing

Source: Internet
Author: User
Tags setf

Original works, reproduced please specify the source: http://www.cnblogs.com/shrimp-can/p/5657192.html

I. Member types

1. ios::fmtflags:

Format flags, commonly used to format output, for functions flags, SETF, unsetf as their arguments or return types.

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 the 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 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 a extracting operation on an input stream.
failbit The last input operation failed because of a error related to the internal logic of the operation itself.
badbit Error due to the failure of a input/output operation on the stream buffer.
goodbit No error. Represents the absence of all the above (the value zero).

3. Other : Sentry, event, Event_callback, failure, Init, OpenMode, Seekdir

Two. Functions inherited from the input stream class IStream

1. gcount: Streamsize gcount () const;

Returns the number of characters fetched from the last non-formatted input operation

  char str[20];  "Please, enter a word: ";  std::cin.getline(str,20);  " characters read: "‘\n‘;
输出为:
Please, enter a word: simplify9 characters read: simplify

2. get: int get (); read a character, often used to exclude the effect of line breaks

istream& get (char& c); read a character to C

istream& Get (char* s, streamsize N); reads a string until it encounters a newline character or reads n-1 characters, and automatically adds the character ' s ' at the end

istream& Get (char* s, streamsize N, Char Delim), reads a string until a character delim is encountered or a n-1 character is read, automatically adds the character ' + ' at the end

Line break is left in the input queue

3. getline:istream& getline (char* s, streamsize N); reads a string until a newline character is encountered or n characters are read (line breaks are also read), and the newline character is replaced with ' \ s '

istream& Getline (char* s, streamsize N, Char Delim); reads a string until the character Delim is encountered or n characters are read, and the newline character is replaced with ' \ s '

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

Extracts characters from the input queue and ignores them until the first n characters are fetched or the character Delim is encountered (Delim is also extracted and ignored). After completion, the first character in the input queue is n+1 characters or delim characters later

  char first, last;  "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  "Your initials are "‘\n‘;

The output is:

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

4. Others : Peek, read, Readsome, putback, Unget

5. Tellg, SEEKG

6. Sync: int sync ();

Input buffer Synchronization

Iii. functions inherited from the output stream

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

Output character C

2. Other: Write, TELLP, SEEKP, flush

Iv. Protection member function (C++11)

= operation, swap

V. Functions inherited from the iOS class

1. iOS::good: bool Good () const;

If the error status identification (Eofbit, Failbit, Badbit) is set to return False if no return ture

2. iOS::EOF: BOOL EOF () const;

If Eofbit is set to return ture, the description arrives at the end of the input

3. fail: BOOL fail () const;

If Failbit or badbit is set to return True

4. bad: bool Bad () const;

If Badbit is set to return True

iostateValue
(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::! Action :

If Failbit or badbit is set to return True

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

Set a new value for the error status ID

7. iOS::fill: Char fill () const; return fill character

Char Fill (char fillch), fill with character Fillch, return fill character

Fill out free space for output

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

Output:

40xxxxxxxx40

8. Others : rdstate, SetState, COPYFMT, exceptions, imbue, tie, rdbuf, narrow, widen

Vi. functions inherited from the Ios_base class

1. flags: Fmtflags flags () const; Returns the current format

Fmtflags flags (fmtflags FMTFL); Sets the new format, returning the previous format

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

Output: 0x64

2. setf: Fmtflags setf (Fmtflags FMTFL);

Fmtflags setf (fmtflags FMTFL, fmtflags mask);

format, return formatting before setting

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

Output:

0x6464

3. unset: void unsetf (Fmtflags mask);

Clear the Mask selection format

4. Precision: streamsize precision () const; Returns the current floating-point precision (that is, the number of decimal places)

Streamsize Precision (streamsize prec); set floating-point precision, return previous floating-point precision

5. width: streamsize width () const; Returns the current field width

Streamsize width (streamsize wide); Sets the field width, returning the previous field width

6. Others: Imbue, Getloc, Xalloc, Iword, Pword, Register_callback, Sync_with_stdio

Reference: http://www.cplusplus.com/reference/istream/iostream/

Input and output stream class iostream common function parsing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.