C + + Primer eighth Chapter Standard IO library __c++

Source: Internet
Author: User

Reproduced from: http://www.cnblogs.com/kingcat/archive/2012/05/09/2491847.html

Before learning the content of this chapter, it is necessary to do a basic understanding of the concept of the buffer, I cited an article on the internet, "C + + programming understanding of the buffer", the contents are as follows:

What is a buffer

A buffer is also called a cache, which is part of the memory space. That is, a certain amount of storage space is reserved in the memory space, which is used to buffer the input or output data, this part of the space reserved is called a buffer.

The buffer is divided into input buffer and output buffer according to whether it is an input device or an output device.

why to introduce a buffer

For example, we take information from the disk, we first put the read out of the data in the buffer, the computer directly from the buffer to take the data, and so on the buffer data to read after the disk, so that can reduce the disk read and write times, plus the computer to the buffer operation greatly faster than the operation of the disk, Therefore, the application of buffer can greatly improve the running speed of the computer.

For example, we use the printer to print the document, because the printer's printing speed is relatively slow, we first output the document to the corresponding buffer, the printer and then gradually print, then our CPU can handle other things.

Now you basically understand that a buffer is a chunk of memory that is used between input and CPUs to cache data. It enables low-speed input and high speed CPUs to coordinate work, to avoid low speed input CPU, freeing up the CPU, so that it can work efficiently.

type of buffer

The buffer is divided into three types: full buffering, row buffering, and no buffering.

1, Full buffer

In this case, the actual I/O operation is completed when the standard I/O cache is filled. The typical representative of full buffering is the reading and writing of disk files.

2, line buffer

In this case, the actual I/O operation is performed when a newline character is encountered in the input and output. At this time, we entered the word Fuxian stored in the buffer, and so on press key to change the line before the actual I/O operation. The typical representative is the keyboard input data.

3, without buffer

That is, no buffering, standard error situation stderr is a typical representative, which makes the error message can be shown directly as soon as possible.

Flushing of Buffers

The following conditions cause a flush of the buffer:

1, buffer full time;

2, execute the FLUSH statement;

3, execute the ENDL statement;

4, close the file.

Visible, the buffer is flushed when the file is full or closed, and the actual I/O operation is done. In addition, in C + +, we can use the flush function to flush the buffer (perform I/O operations and empty the buffer), such as:

  Cout<<flush ; Output video memory content immediately to display

  

  Cout<<endl ; The Endl control character is to move the cursor to the beginning of the next line in the output device and empty the buffer

Equivalent

  cout<< "\ n" <<flush ;

Illustrate by example demo

1, file operation demo Full buffer

Create a console project and enter the following code: #include <fstream>
using namespace Std;

int main ()
{
Create file Test.txt and open

ofstream outfile ("test.txt");

Write 4,096-character ' a ' to the Test.txt file

for (int n=0; n < 4096; n++)
{
OutFile << ' a ';
}

Pause, press any key to continue

System ("PAUSE");

Continue writing the character ' B ' to the Test.txt file, which means that the No. 4097 character is ' B
'
outfile << ' B ';

Pause, press any key to continue

System ("PAUSE");

return 0;
}

The above code is easy to understand and has been commented inside the code.

The purpose of writing this small code is to verify that the total buffer size of Windows XP is 4,096 bytes (the reprint Note: Under Windows7, the Codeblocks platform under the experiment is 510 bytes ), and verify that the buffer full will flush the buffer, execute the real i/ O operation.

Compile and execute, running the results as follows:


When you open the Test.txt file in the project's folder, you will find that the file is empty, indicating that the 4,096 character "A" is still in the buffer and does not actually perform I/O operations. Hit the Enter key and the window changes to the following:


When you open the Test.txt file again, you will send a 4,096 character "a" in the file. This means that the total buffer size is 4K (4096), the buffer is full and I/O is executed, and the character "B" is still in the buffer.

Tap the return key again, and the window changes to the following:


When you open the Test.txt file again, you will find that the character "B" is also in it. This step verifies that the buffer was refreshed when the file was closed.

2, keyboard operation demo Line buffer

Introduce the getchar () function first.

Function prototype: int getchar (void);

Note: When the program calls the GetChar () function, the program waits for the user to press the key, and the character entered by the user is stored in the keyboard buffer until the user presses the carriage return (the carriage return character is placed in the buffer). The GetChar () function begins to read one character at a time from the keyboard buffer when the user types in a carriage return. That is, subsequent getchar () function calls do not wait for the user to press the key, but read the characters in the buffer directly until the characters in the buffer are read to wait for the user to press the key again.

Do not know you understand no, again popular point of speaking, when the program calls the GetChar () function, the program waits for the user to press the key, and so the user presses enter return. The characters that are pressed during the period are stored in the buffer, and the first character returns the value as a function. Continue calling the GetChar () function, which will not wait for the user to press the key, but return the 2nd character you just entered; continue the call, return the 3rd character until the character in the buffer is read before waiting for the user to press the key.

If you do not understand, can only blame my ability to express limited, you can combine the following examples to experience.

Create a console project and enter the following code: #include <iostream>
using namespace std;  



Int main ()
{

  char C;
    

    //calls the GetChar () function for the first time, and when the program executes, you can enter a string of characters and press ENTER. The function returns when the ENTER key is pressed. The return value is the first character entered by the user (assuming the user enters  abcdef, the function returns a)

  c = getchar ();

    //Displays the return value of the GetChar () function

  cout<< c  << endl;   Output  a  


    //  loops call the GetChar () function multiple times, displaying the return value of each call to the GetChar () function. The carriage return was not finished until it was encountered.   At this point the function execution does not let the user enter but sequentially reads the buffer character contents. The first character is read at the end of the user input, so the

   while ((C = getchar ())!= ' \ n ')
is read at the beginning of the second character    {
       cout<<  ","  << c <<endl 
  &NBSP;}

   return 0;
}

This small piece of code is also very simple, but also inside the code there are comments. The final output is

a

, b

, C

, D

, E

, F

  

  The execution of the GetChar () function takes the line buffer. The first call to the GetChar () function allows the program consumer (user) to enter a line of characters and return until the ENTER key function is pressed. The characters and carriage returns entered by the user are now stored in the row buffer.

Calling the GetChar () function again will progressively output the contents of the row buffer.

3, standard error output with no buffer

Use when error output:

  cerr<< "error, please check the input parameters!" ;

This statement is equivalent to:

  fprintf (stderr, "error, please check the input parameters!") ;

After reading the buffer content we will learn the C + + standard IO library.

8.1 Object-oriented annotation Library

First look at the IO Operation class diagram:

The IO library is roughly operable with three types of data: the console stream (stream), files (file), and Strings (string).

The type of operation can be divided into three categories: Input (in), Output (out), input and output (in/out).

Ostream is the base class for all types of output operations

It expands out two subclasses: ofstream The output action class for the file;ostringstream Output action class for string

IStream is the base class for all type input operations

It expands out two subclasses: ifstream The input action class for the file;istringstream Input action class for string

ostream and IStream together to expand the class iostream It is responsible for processing the input and output of the console stream

iostream extends two subclasses:stringstream specifically handles string input and output, fstream Specialized processing of file input and output

All classes are defined in different header files, and proper use of classes must introduce corresponding header files

The iosteam header file defines :
IStream(read from the stream);
ostream(written to the stream);
iostream(convection for reading and writing, derived from IStream and Osteam)


The fsteam header file defines :
ifstream(read from file, derived from IStream);
ofstream(written to the file, derived from the ostream);
fstream(Read and write to the file, derived from the iostream);


The ssteam header file defines :
Istringstream(read from a string object, derived from IStream);
Ostringstream(written to a string object, derived from ostream);
StringStream(read and write to a string object, derived from iostream);

To use the IO class correctly, you must understand their correspondence and the responsibilities of each class.

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.