Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ C ++ input and output summary _ Input     - CentOS and RHEL installation under GAMIT10.6 (Linux)

- 24 Docker recommendations (Linux)

- Python basis: os module on the file / directory using methods commonly used functions (Programming)

- MySQL Parameter Tuning Best Practices (Database)

- Oracle database NUMBER (x, y) data types (Database)

- Advanced Linux security settings (Linux)

- Use Ganglia to monitor your cluster (Server)

- Let OS X support NTFS write file (Linux)

- Oracle online redefinition (Database)

- Exploring the Android Listview display confusion (Programming)

- Oracle SDE and maintain common commands - Display space (Database)

- Upgrading KDE Plasma 5.3 in Ubuntu 15.04 (Linux)

- SQLite (Database)

- SecureCRT connect virtual machine Linux system (Ubuntu) (Linux)

- Copy and paste in Linux terminal and Vim (Linux)

- JavaScript basic tools list (Programming)

- To setup CentOS LAMP environment (Server)

- How to Install Xombrero 1.6.4 (minimalist Web browser) on Ubuntu and Archlinux (Linux)

- Based on OpenSSL for HTTPS service configuration (Server)

- Java input and output common class Scanner (Programming)

  C ++ input and output summary _ Input
  Add Date : 2017-09-03      
  1. The nature of the input and output

  In C ++ input and output streams are carried out by, and specific input and output operations are carried out by convection to complete, typically directed a stream (redirection), clear streams, add new elements to flow inside. C ++ seen as the input and output byte stream to extract bytes from the byte stream input, the output byte into the byte stream.

2. Use cin and input method

cin object represents the standard input stream of bytes is usually the case with such a keyboard to generate a stream of bytes, the commonly used method to send input cin cin >> abc; and istream classes were overloaded extraction operator, is the you can identify these basic types, unsigned / signed char &, char &, short &, unsigned short &, int &, unsigned int &, long &, unsigned long &, long long &, unsigned long long &, float & double &, long double &; for these basic types directly plus with cin extraction operator >> can be achieved. One problem is that the extraction operator >> skips back and ignore newline similar spaces, tabs like the character, so this is a procedure in question, the code inside the loop will always run down , simply by this method of data input to the character class appeared big problem.

For if you want to enter a string cin >> input; this method to input a string, then encounters a blank line breaks, tabs, and the like, input will stop

 1 char a;
 2 int times = 0;
 3 cin >> a;
 4 while (a! = '\ N')
 5 {
 6 cout < < a;
 7 cin >> a;
 8 times ++;
 9 }
10 cout < < times;
3. c ++ string, character, string type input method // what a good solution to the blank, line breaks, etc. are not read the situation.

C ++ for the time string, character and string input common type of get (), getline () function, they are called non-formatted input, because they only read the characters, including white space, line breaks, tabs, etc. nor data conversion, read what is what.

get inside the istream defined () function getline () function:

Get with a single character input, in the case of parameters or without parameters, get input function reads a character, no matter what the character is a newline, tabs can be directly read into it.

Returns which get (void) will be converted into character after shaping, for get (char &) function returns a pointer to call it the istream object reference, so that you can use similar cin.get (a) .get (b) .get (c); ways to read, pay attention to get this one does not ignore newline, so pay attention to line breaks are not careful reading into the situation.

If you read down the end of the file, or the tail of keyboard input, etc. not the case when something readable, cin.get (ch); return false, the rest are normal returns true.


Compare cin.get () and cin.get (ch) of
Characterized cin.get (ch) ch = cin.get ()
Transmission input feature directly assigned to the return value of the function assigned to the ch ch
After reading the character function returns istream object reference to point to the character encoding actually int value
The return value is reached the end of the file is converted to false EOF function
NOTE: Keyboard emulation input files end with the same effect as the real end of file

get () function

get in istream defined () function, a total of six heavy duty: use when they meet any of the arguments to rule

1. int_type __CLR_OR_THIS_CALL get (); // no parameters, read only one character, the return value of type int

2. _Myt & __CLR_OR_THIS_CALL get (_Elem * _Str, streamsize _Count); // takes two arguments, the first address of a string, an int parameter to control the maximum number of characters read, for storing the end of the string // / 0, this parameter string freshman than read, the function default delimiter is / n.

3. _Myt & __CLR_OR_THIS_CALL get (_Elem * _Str, streamsize _Count, _Elem _Delim); // the top basically the same, except that the function delimiter for their given _Delim.

4._Myt & __CLR_OR_THIS_CALL get (_Elem & _Ch); // receive parameters of type char reference works is to assign values directly ch;

// Find a little time to test the function, the string can not be referenced, because the array is composed of several elements of a composed collection, it is impossible to build an array of aliases.

If this is a char & bbb = input [10]; // reference to the array can not be established.
                           // This is the effect of the input [0] referenced.

5._Myt & __CLR_OR_THIS_CALL get (_Mysb & _Strbuf);

6._Myt & __CLR_OR_THIS_CALL get (_Mysb & _Strbuf, _Elem _Delim); // In addition to the first function of the other function return values are directed to call its istream object.

The above function when using ideas should only be used cin.get (several parameters or not), get () function in the event of a line break after reading the character and line breaks or does not read the original default delimiter , and these things will only remain in the original stream,

char input [10]
char ch;
cin.get (input, 10, 'a');
cout < < input < < endl;
cin.get (ch);
cout < < ch < < endl;
Enter sdsdsda

Output sdsdsd


getline () function

among the istream getline () function overloading total of two

 1. _Myt & __CLR_OR_THIS_CALL getline (_Elem * _Str, streamsize _Count);
 2. _Myt & __CLR_OR_THIS_CALL getline (_Elem * _Str, streamsize _Count, _Elem _Delim);

These two functions are similar to the get () function in the 2, 3 Reload, the difference is that getline () function will be automatically discarded line breaks and other delimiters.

char input [10]
char ch;
cin.getline (input, 10, 'a');
cout < < input < < endl;
cin.get (ch);
cout < < ch < < endl;
Enter sdsdsda

Output sdsdsd


Because discarded delimiter is a delimiter is discarded following cin.get (ch) read is a newline, so the output when the output of just a newline.

Input to the string class

Simply should cin >> to input a string string class, you will encounter spaces, tabs, line breaks and other stops.

With getline () to read the string class, getline () function is defined in vs2010 in the string file, a total of two reconstruction, and are inline functions

1. template < class _Elem,
   class _Traits,
   class _Alloc> inline
   basic_istream < _Elem, _Traits> & getline (
       basic_istream < _Elem, _Traits> & _Istr,
       basic_string < _Elem, _Traits, _Alloc> & _Str,
       const _Elem _Delim) // using their own defined delimiters, the parameters for the directional flow class, and the second parameter is an output target string, the third parameter is the custom delimiter

2. template < class _Elem,
   class _Traits,
   class _Alloc> inline
   basic_istream < _Elem, _Traits> & getline (
       basic_istream < _Elem, _Traits> & _Istr,
       basic_string < _Elem, _Traits, _Alloc> & _Str) // default delimiters is \ n

These two functions are not the maximum number of characters in the parameter string, because the string class on their own according to the size of the string automatically resize.
- Fedora 22 installation and configuration optimization (Linux)
- Linux System Getting Started Tutorial: How to Force Change your password at next logon Linux (Linux)
- Grep, Ack, Ag searches Efficiency Comparison (Linux)
- How do I upgrade to Ubuntu 15.04 (Beta) (Linux)
- Netapp storage routine inspections and information gathering (Linux)
- Ubuntu 12.04 / 14.04 users to install software LyX document processing (Linux)
- Spring MVC Exception Handling (Programming)
- MySQL thread cache thread_cache_size parameter optimization (Database)
- Oracle multi-user concurrency and transaction processing (Database)
- Several reasons MySQL garbled (Database)
- How to install Visual Studio Code on Ubuntu (Linux)
- SQLite3 simple operation (Database)
- Ubuntu firewall installation and configuration (Linux)
- MySQL 5.6 use GTIDs build the master database (Database)
- C ++ pointer two third memory model (Programming)
- Linux rpm command Detailed (Linux)
- How a lot of blocking malicious IP address in Linux (Linux)
- to install the deployment of LVS under CentOS 7.0 (Server)
- MySQL Statistics (Database)
- Linux Command Tutorial: cat command to view the contents of the file (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.