Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ C ++ input and output summary _ Input     - Towards Docker (Server)

- Oracle 11g principle study Dataguard (Database)

- Oracle ORA-01089 failure analysis (Database)

- Linux beginners should know 12 commands (Linux)

- Lua non-blocking write log (Programming)

- Unity Greeter Badges: the lost session icon back to the login screen Ubuntu (Linux)

- Java object serialization (Programming)

- Use Spring cache and ehcache (Programming)

- Linux remote connectivity tools -OpenSSH (Linux)

- CentOS / Linux kernel upgrade (Linux)

- CentOS 6.6 permanent method to modify the DNS address (Linux)

- Linux scp remote file copy (Linux)

- The file name is garbled or deleted files with special characters under Linux (Linux)

- How to use the command line ftp upload and download files (Linux)

- Mass data storage application of MongoDB database (Database)

- CentOS install Java 1.8 (Linux)

- C ++ two second pointer memory model (two-dimensional array) (Programming)

- to install the deployment of LVS under CentOS 7.0 (Server)

- C language preprocessor command (Programming)

- Django template inheritance child (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.
- 11G ASM disk group does not automatically handle MOUNT (Database)
- The relationship between UNIX and Linux (Linux)
- X security settings in Ubuntu (Linux)
- CentOS 7.0 running Docker kernel error solution (Server)
- Linux iptables: Composition Rules (Linux)
- Using open source software to build XWiki Wiki system installed within the company (Linux)
- MySQL monitoring tool -Innotop (Database)
- Samba file sharing server set up (Server)
- Composition and dynamic memory allocation C program (Programming)
- Keepalived + HAProxy high availability load balancing (Server)
- linux smartd [FAILED] appears at startup (Linux)
- Ubuntu 14.04 Configuring cuda-convnet (Linux)
- Oracle 11g user rights management study notes (Database)
- Linux configuration startup mount: fstab file (Linux)
- By creating a personal technology blog Detailed GitHub (Linux)
- The multiplexed signal driving IO (Programming)
- Using Java program determines whether it is a leap year (Programming)
- Reset CentOS / RHEL root account password 7 (Linux)
- Hadoop2.0 configuration yarn success (Server)
- Teach you to diagnose problems with strace (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.