Chapter 1 Standard I/O Library

Source: Internet
Author: User

 

1. introduce several common functions first

1) getline (istream is, string s) stores the characters read from the input stream is \ n, or the end of the file or reading errors. It is equivalent to reading a line of characters into s.

2) getline (istream is, string s, char c) stores the characters read from the input stream is always ended with c, or the end of the file or the reading error) in string s.

For example, getline (cin, str ,'#')

3) getchar () is equivalent to getc (stdin) reading a character from the input stream.

4). cin. getline (char * cha, int num, char f)

Read num characters from the standard input stream cin or encounter an ending character f. char f ends with \ n by default. Store the read characters in cha.

For example, char cha [10]; cin. getline (cha, 5, '*'); cout <cha <endl;

5), string s; cin> s; read a string from the standard input stream and put it into s.

 

2. Object-oriented standard library

IO type: Standard Input/Output Window, read/write file, and string in memory.

IO objects cannot be copied or assigned values. Therefore, a reference symbol must be added as a function parameter or return value.

1) standard input/output header file <iostream>

Standard input stream: Object cin in standard library of istream class

Standard output stream: Object cout in ostream standard library

Standard input/output streams: The iostream class inherits from the istream class and ostream class.

 

3. Condition status

S. eof () s. fail () s. bad () s. good () s. clear () s. clear (flag) s. setstate (flag) s. rdstate ()

Flag: strm: iostatestrm: badbitstrm: failbitstrm: eofbit

For example, while (cin. eof () such as ifstream: iostate infile_state = ifstream: badbit;

S. clear (eofbit)

How to refresh the buffer

1,

1) cout <"hi" <endl; additional operation line feed

2) cout <"hi" <flush; no additional operations

3) cout <"hi" <ends additional operation inserts null characters

2. unitbuf Operator

Cout <unitbuf <"first" <"second" <nounitbuf;

Equivalent

Cout <"first" <flush <"second" <flush;

Nounitbuf restores the operation stream to normal, which is refreshed by the System-managed buffer.

 

4. file input and output header file <fstream>

Ifstream: Derived from istream, provides the File Read function.

Ofstream: Derived from ostream, provides the file writing function.

Fstream: Derived from iostream, provides the ability to read and write the same file.

Two operations, open and close, are defined at the same time.

Ifstream infile (ifile. c_str () // The real parameter must be a c-style string. When creating a write object, bind the object

Ifstream infile; infile. open (ifile. c_str () // bind it later. File streams can be rebound. Before a file stream is rebound to another file, you need to disable the file currently bound.

Ifstream infile ("in ");

Infile. close ();

Infile. open ("next ");

Common operations to clear file streams:

In. close (); in. clear ();

Common operations to clear the input buffer:

In. ignore (200, ''); skips the 200 characters in the input stream or ends with a space.

In. ignore () skips 1 character by default.

File mode:

In: open the file read operation

Out: open the file write operation

App: Find the end Of the file before each write

Ate: After the file is opened, the file is immediately located at the end of the file.

Trunc: Clear all contents of the file after opening the file

Binary: open a file in binary mode

Out, trunc, and app are used for files associated with ofstream and fstream.

In can only be used for ifstream and fstream files.

Ate binary can be used for all files.

 

5. String stream header file <sstream>

Iostream Standard Library supports input/output in memory. You only need to bind the stream with the string object in the memory of the program.

Istringstream: Derived from istream, which provides the function of reading strings.

Ostringstream: Derived from ostream, which provides the string writing function.

Stringstream: Derived from iostream and provides the read/write function.

Common Operations:

Stringstream strm; // create a free stringstream object.

Stringstream strm (s); // create the stringstream object that stores the copy of s, where s is a string object

Strm. str (); // return the string type objects stored in strm.

Strm. str (s); // copy string type s to strm and return void

Common Methods:

1) read every word in the file

String line, word;

While (getline (cin, line) // read a line of Characters in the file and store it in line.

{

Istringstream stream (line); // use the string in line to initialize the string stream.

While (stream> word) // read each string in the string stream in sequence

// Do something

}

(2) format conversion

Cin cout is equivalent to scanf printf in C to format input and output.

Int I; cin> I; read an integer data from the input stream cin Based on Integer I.

Int val1 = 512, val2 = 1024;

Ostringstream format_message;

Format_message <"val1:" <val1 <"\ n"

<"Val2:" <val2 <"\ n ";

It is equivalent to storing the string val1: 512 \ n val2: 1024 to the string as two rows)

Then the corresponding integer value can be read according to a certain form.

Istringstream input_istring (format_message.str ());

String dump;

Input_istring <dump <val1 <dump <val2;

Cout <val1 <val2; // only extracts the integer 512 and 1024 from the input stream. dump is only an auxiliary function and removes the corresponding string.

Assign the data in the input stream input_istring to the corresponding variable based on the variable type.

 

 

This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1290428

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.