Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ C ++ input and output summary _ Input     - The Oracle VM VirtualBox use U disk under ubuntu (Linux)

- Use source packages compiled and installed GCC5.1 in Mac OS X 10.10.3 (Linux)

- Oracle 11g through SCN do incremental backup repair standby library detailed process (Database)

- Linux Live CD lets your PC is no longer secure (Linux)

- The Sublime Text 3 configuration file (Linux)

- JBPM6 Tutorial - taught you how to install JBPM (Linux)

- Linux - use chroot command (Linux)

- Linux system using the command line shutdown or restart (Linux)

- Linux Firewall IPCop Profile (Linux)

- Java Set and List in the relationship and difference (Programming)

- How to fix the Ubuntu Error script returned error exit status 1 (Linux)

- To create someone else can not afford to delete the administrator user (Linux)

- installation of Vim plugin YouCompleteMe under Ubuntu 15.04 (Linux)

- How to Start a Linux application running in the background using the terminal mode (Linux)

- 25 Git Usage Tips (Linux)

- Android Get App version number and version name (Programming)

- To install the Oracle process notes on Oracle Linux 4u4 (Database)

- How to Install Telegram instant messaging software on Ubuntu (Linux)

- Repair Chrome for Linux is (Linux)

- CentOS 7 update source - the use of domestic sources 163 yum (Linux)

  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.
- Getting Started Linux Shell Scripting (Programming)
- Linux network security backdoor technology and advanced skill practice (Linux)
- Automated Password Generator: Linux under a special password generator (Linux)
- Ubuntu install Eclipse for C / C ++ and related configuration (Linux)
- CentOS 6.5 dual card configuration, one of the external network, a local area network connection (Linux)
- MySQL 5.7.9 source compiler installation instructions (Database)
- The multiplexed signal driving IO (Programming)
- JavaScript notes --Objects (Programming)
- Ubuntu server 8.04 Firewall Guide (Linux)
- SendMail version of Java implementation with attachments (Programming)
- Ubuntu 14.04 configure JDK1.8.0_25, switchable version (Linux)
- I like Linux Security (Linux)
- Android and OpenCV2.4.4 build an image processing framework (2013 edition) (Linux)
- Source install Python3.4 on CentOS (Linux)
- Use Git in Eclipse (Linux)
- Hardware Firewall Basics (Linux)
- Try to use Lets Encrypt (Linux)
- Linux system using the command line shutdown or restart (Linux)
- Linux ban single-user mode to enhance system security (Linux)
- Oracle SQL statement tracking (Database)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.