C + + IO class, file operations

Source: Internet
Author: User

Tag: Val Get ABC must be equivalent to HTM getline character execution

C + + IO class mainly divided into three kinds

Blog post reference: http://www.cnblogs.com/yxnchinahlj/archive/2011/02/12/1952546.html

Input: From device (file) to buffer--memory

Output: Memory-to-buffer-to-device (file)

1, corresponding to the standard input and output (console) IStream, Ostream, iostream header file include<iostream>2, the corresponding file input and output ifstream, Ofstream, fstream header file include<fstream>3, corresponding string input and output Istringstream, Ostringstream, stringstream header file include<sstream>

IStream, Ostream, Osstream class

1. Typical Object cin

(1), CIN >> operator reads data according to the type of the subsequent variable.

Enter end condition: Encounter Enter, Space, TAB key.

Handling of Terminator: Discard Terminator (Enter, Space, TAB) in the buffer that makes the input end

A, when reading a character, enter more than one character to read a single character. Other characters continue to remain in the buffer.

Char A, B;

Cin >> A;

CIN >> B;

Enter QEWR carriage return//A<--Q,B<--E

If the type does not match, CIN is faulted. If CIN >> intval, enter ' a ', cin error

int i;

Cin >> I;

Input: A return//Outflow error

b, read the string, you must ensure sufficient storage space, or error

Char c1[5];

Char c2[5];

CIN >> C1;

CIN >> C2;

Input ABCDE Enter//error can only receive 5-1 = 4 characters, and automatically add null characters at the end. C1[4] = = 0

(2), cin.get ()

The function has three formats: no parameter, one parameter, two parameters

i.e. Cin.get (), Cin.get (char ch), Cin.get (Array_Name, Arsize)

To read a character:

Enter end condition: Enter key

Processing for Terminator: Do not discard the enter in the buffer

Cin.get () and Cin.get (char ch) are used to read characters, their use is similar,

That is, Ch=cin.get () is equivalent to Cin.get (CH).

  To read a string:

     Cin.get (Array_Name, arsize) is used to read a string, can accept a space character, encounters a enter end input, reads characters by length (arsize), and reads Arsize-1 characters. The last enter character is discarded.

Char C1[5]

CIN >> C1;

Input: 123,456 return car//c1 [1][2][3][4][(null character)];

(3). cin.getline ()

Cin.getline () and Cin.get (Array_Name, arsize) read the same way, ending with enter, you can accept the space character. Reading a character by length (arsize) discards the last enter character.

But there are differences between the two functions:

Cin.get (Array_Name, arsize) when the input string is very long, it does not cause an error in the CIN function, and subsequent CIN operations continue, just fetching data directly from the buffer. However, cin.getline () will cause an error in the CIN function when the input is very long, and the subsequent CIN operation will no longer execute.

C + + IO class, file operations

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.