C ++ day13 Study Notes

Source: Internet
Author: User

1. Data Memory Variables
Files on disk

2. Move data from other devices to the memory --- input --- read
Put data in memory into other devices --- output --- write

3. Stream
Object-oriented movement, and data-oriented movement in the input and output streams
Source of the input stream: file destination: Memory
Source of the output stream: Memory destination: File

4. standard output device-Display
Standard Input Device-keyboard

Keyboard --- memory --- display
|
Input operation output operation

Input/output stream: between memory and disk, between memory and standard input/output device

5. cout Source: Variable destination: Display
A variable in the memory of the CIN keyboard

6. An object of the standard input stream CIN istream
An object of the standard output stream cout ostream
The cerr destination of the standard error stream is a screen and replaced by cout.

7. Cin
Is a standard input object with buffer, the default input device is the keyboard
(1)>: automatically checks the data type.
If you press enter, Data Reading starts. If you encounter a space, Data Reading ends. You can only read one word.
The stream is the targeted movement of data, and the read data disappears. The data that has not been read will remain in the stream until the Stream disappears and the data disappears.
If there is data in the stream, it will block and wait for reading --- all input method features
Why ">" can be used continuously? Because the returned result is a reference to the istream object itself.
Note: ">" do not read carriage return, do not read Spaces
(2) Get (): one character is read each time. Returns an integer, which is actually an ascii code for reading characters.
Read the carriage return and space as common characters.
(3) Get (char &): stores the read content in the Parameter
Cin. Get (ARR [0]). Get (ARR [1]); // get (char &) returns the CIN itself, which can be used continuously
(4) Getline (STR, 256): reads a row, including spaces.
For carriage return, read-only, not saved
Will read the array length minus 1 character, the last put '\ 0'
Input data exceeds the given space (1) truncate the data, and the remaining data is still in the stream
(2) set an error mark, call cin. Clear (), clear the error, and continue working.

View code

# Include <iostream> Using   Namespace  STD; Int  Main (){  Int  Age;  Char Name [ 20  ]; Cout < "  Enter your age>  "  ; CIN > Age; cin.  Get (); // Read the residual carriage return in the stream so that Getline can work normally.  //  Cin. Ignore (20, '\ n ');  //  Ignore 20 characters or press enter to clear from the stream Cout < "  Enter your name>  "  ; Cin. Getline (name,  20  ); Cout < "  Your age is:  " <Age <Endl; cout < "  Your name is:  " <Name < Endl ;} 

(5) read (char *, INT) char * is the address of the stored result, Int Is the read length, and cannot be larger than the space of the preceding array.
Special characters can also be processed as common characters until they are fully read.
The excess part still exists in the stream.
As long as the data is not fully read, it is always blocked.
'\ 0' is not automatically supplemented. When passing parameters, the length of the array is reduced by 1 and' \ 0' is supplemented to avoid garbled characters.

Common Features of all input streams: blocking as long as there is no data
If you can't finish reading the data, you can leave it in the stream.

(6) cin. ignore (255, '\ n') ignores 255 characters or encounters' \ n'. If '\ n' appears in the first 255 characters ', the characters before '\ n' are ignored.
(7) PEEK () to view the first character in the stream
View only, not read away
(8) putback () inserts a character into the stream, provided that there is space available
Must be used with get () and get () to get a character before putback () can be used to insert a character.
(9) cin. Fail () determines whether an error mark is displayed. If everything is normal, false is returned.

When the data type read with CIN is incorrect, this is an unrecoverable error. The error mark cannot be cleared with CIN. Clear ().
To read data on the keyboard, use Getline () to ensure the normal formation of the input stream. After reading the data, use the forced conversion type to obtain the required data.

8. ifstream
(1) # include <fstream> header files
Ifstream ifs ("test.txt"); // create an ifstream object, open the file, and pass it to the constructor as a parameter, that is, the file name to be opened
// Find the file in the current directory, or use the relative or absolute path to find the file
After opening the file, immediately determine whether the file is successfully opened

  If   (IFS. fail () {cout   "  can't open test  "   Endl;   return   0  ;}  If  (! IFS) {///   You can also determine whether the file is opened incorrectly  cout "   can't open test  "   Endl;   return   0  ;} 

(2) there will be an "EOF" sign at the end of the file, as the end of the file.
You can determine whether to read "EOF" to determine whether to read the end of the file.

 
If(IFS. EOF ()){Break;}

(3) There are several files that can be opened by a process, so the file is a resource.
After use, close the file input stream.

Exercise: (1) print the/etc/passwd file
(2) read the content in the file that does not start "#"
Peek (), ignore ()

9. Output operation cout
(1) <operations that are output to the screen can only be output, all of which are output as strings.
That is to say, this operation has the function of automatic type conversion.
(2) Put () returns the cout reference and can be called continuously.
(3) write (BUF, Len) to the screen according to the specified length Buf is char * type
(4) cout. Width (10); the printed content occupies a total of 10 characters and is right aligned.
Effective only for one cout behind it
(5) cout. Fill ('#'); fill the space '#'
It is called once and will take effect for future calls.
(6) SETF () operation, control format, teaching material page 1

(7) special characters
'\ R' indicates to roll back a grid and then output
'\ N' press ENTER
\ Outputs a '\' because '\' is considered an escape character
'\ T' a tab key

(8) Output Controller
Oct outputs "0" by octal"
Decimal output
Hex outputs "0x" in hexadecimal format"
Flush clears the buffer zone because it negotiates with external devices. This reduces the number of outputs to the screen and improves efficiency.
Press enter, Program Both end and flush are cache refresh commands.
Cout <"A" <flush;

10. ofstream
Open the file, write the data into the file, and close the file.

 
Ofstream ofs ("Ofstream.txt");//Open the file. If the file does not exist, create it.If(OFS. Fail ()){//An error occurred while writing the file, which is generally caused by permission issues.Cout <"Open File Error"<Endl;Return 0;}

The CIN cout object in the iostream header file has been declared and can be used directly, because the standard input/output device is unique and the system declares
However, the fstream object must be declared by the programmer, because the input and output to the file are not unique.

Ofstream ofs ("ofstream.txt", IOS: APP); // write to the file as an append
IOS: trunc clears the file content and writes a new file by default.
IOS: nocreate do not create new files
IOS: noreplace is not rewritten. You need to create a new file.
Combine multiple ofstream ofs ("ofstream.txt", IOS: app | IOs: In | IOs: Binary );

11. Read and Write binary files
(1) iOS: Binary
(2) The read/write method reads and writes binary files, because the two methods only need to start the bit and size to work.


Job: (1) int readint (){}
Double readdouble (){}
Strong conversion functions, strong fault tolerance and reliable means to notify users of errors
(2) print the user ID, home, and Shell Based on the user name entered by the user. If the user does not exist, notify the user.
Strtok (), strcmp ()

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.