Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ input and output summary _ Input     - Ubuntu prevent arp attacks (Linux)

- Ubuntu 14.04 to install Blender 2.71 (Linux)

- Getting Started with Linux system to learn: how to check memory usage of Linux (Linux)

- Linux_Logo - output color ANSI Linux distributions logo command-line tool (Linux)

- Ubuntu 14.10 / Linux Mint 17.1 Win10 compiler install rdesktop 1.8.3 Remote Desktop connection (Linux)

- The YUM package management under Linux (Linux)

- CentOS RedHat YUM Source Extensions Supplement (including 32-bit, 64-bit) (Linux)

- JDK comes with tools JPS (Linux)

- C # socket udp broadcast (Programming)

- SSH service configuration to use a certificate login Linux server (Server)

- Spacewalk Linux system configuration and installation (Linux)

- Generators and iterators in Python (Programming)

- Using shell users or virtual users to login to pureftpd (Linux)

- Linux system Passwd file detailed analysis (Linux)

- 10 practical Java programming technology (Programming)

- Commonly used Linux system camouflage method (Linux)

- Bubble Sort Algorithms (Programming)

- List Leaves (Programming)

- Oracle delete table space prompted ORA-00604 and ORA-38301 (Database)

- ElasticSearch basic usage and cluster structures (Server)

 
         
  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

a

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.
     
         
       
         
  More:      
 
- Spring AOP custom annotation way to achieve log management (Programming)
- Docker improve safety (Server)
- Let Git variety of colors and custom log format output (Linux)
- Use window.name + iframe cross-domain access to data Detailed (Programming)
- Linux network security backdoor technology and advanced skill practice (Linux)
- Shell Script: create a function and specify a directory for download (Programming)
- HAProxy performance under high concurrency (Server)
- Terminal multiplexing tool tmux use (Linux)
- GO five stages of language learning (Programming)
- Linux LVM space expansion Demo (Linux)
- Linux Shell Scripting (Programming)
- Ubuntu Froxlor Server Administration panel installation (Server)
- Search Linux commands and files - which, whereis, locate, find (Linux)
- CV: Linux command displays the progress of the run command (Linux)
- Using open source software to build XWiki Wiki system installed within the company (Linux)
- JDK comes with tools jinfo (Linux)
- Database Blob data type conversion String (Programming)
- 25 Git Usage Tips (Linux)
- How to install Eclipse Luna IDE on CentOS 7 / RHEL 7 (Linux)
- Ubuntu and Archlinux install Notepadqq 0.50.2 (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.