Introduction to C++ input streams and output streams

C++ can also be called "C with classes", which means that C++ is based on the C language and adds object-oriented (classes and objects). On this basis, readers who have learned C language should know that it has a complete set of solutions for completing data reading and writing (I/O):

  • Use functions such as scanf() and gets() to read data from the keyboard, and use functions such as printf() and puts() to output data to the screen;

  • Use functions such as fscanf() and fgets() to read data from the file, and use functions such as fprintf() and fputs() to write data to the file.

You know, this set of I/O solutions in C language is also applicable to C++ programs, but C++ is not "lazy". It has independently developed a new set of I/O solutions, which includes the cin that everyone has been using. and cout. In the previous chapters, we have been using cin to receive data input from the keyboard, and cout to output data to the screen (these two processes are collectively referred to as "standard I/O"). In addition, C++ also supports reading data from files and writing data to files (collectively referred to as "file I/O").

Essentially, this C++ I/O solution is a class library containing many classes (as part of the C++ standard library), which are often called "stream classes".

C++ developers believe that the process of data input and output is also a process of data transmission. Data flows from one place to another like water, so this process is called "stream" in C++, and the class that implements this process is called " Streaming category".

These stream classes used to implement data input and output in C++ and the relationship between them:

Among them, the arrows in the figure represent the derivation relationships between various classes. For example, ios is the base class for all stream classes, and it derives istream and ostream. In particular, in order to avoid the ambiguity of multiple inheritance, the virtual keyword (virtual inheritance) is used when istream and ostream are derived from ios.

The respective functions of these flow classes are:

  • istream: often used to receive data input from the keyboard;

  • ostream: often used to output data to the screen;

  • ifstream: used to read data from files;

  • ofstream: used to write data to files;

  • iostream: Inherited from the istream and ostream classes, because the function of this class is both in one, and can be used for both input and output;

  • fstream: It combines the functions of ifstream and ofstream, and can both read data in files and write data to files.

C++ input and output streams

In the study of the previous chapters, as long as input or output data is involved, what we immediately think of is cin and cout. In fact, cin is an object of the istream class, and cout is an object of the ostream class. They are both declared in the <iostream> header file. This also explains "why you can use cin and cout by introducing <iostream> in a C++ program" (of course To use cin and cout, you also need to declare the std namespace).

In addition, there are two ostream class objects declared in the <iostream> header file, namely cerr and clog. Their usage is exactly the same as cout, but cerr is often used to output warning and error messages to program users, and clog is often used to output log information during program execution (this part of the information is only visible to program developers and does not need to be exposed to ordinary users). public).

The differences between cout, cerr and clog are as follows:

  1. In addition to outputting data to the screen, cout can also output data to a specified file through redirection (which will be discussed later); while neither cerr nor clog support redirection, they can only output data to the screen;

  2. Both cout and clog are equipped with buffers, that is, when they output data, they will first put the data into the buffer, and wait until the buffer is full or manual line wrapping (using the newline character '\n' or endl), then all the data will be put into the buffer. Displayed on the screen; cerr does not have a buffer, it will output the data directly to the screen.

Except for the above two characteristics, there is no difference between cerr, clog and cout. The reason why we commonly use cout is because cerr and clog have different applicable scenarios. Taking cerr as an example, once cerr is used to output data somewhere in the program, we will naturally think that the output here is a warning or error message.

It is worth mentioning that objects like cin, cout, cerr and clog are all created by developers of the C++ standard library and can be used directly. Such objects created in advance in C++ are called built-in objects. In fact, the <iostream> header file also declares four built-in objects for processing wide characters, namely wcin, wcout, wcerr and wclog. Since they are not the focus of this section, they will not be explained in detail here.

The following program demonstrates the basic usage of cin, cout, cerr and clog:

    #include <iostream>
    #include <string>
    int main() {
        std::string url;
        std::cin >> url;
        std::cout << "cout:" << url << std::endl;
        std::cerr << "cerr:" << url << std::endl;
        std::clog << "clog:" << url << std::endl;
        return 0;
    }

The program execution result is:

http://c.biancheng.net
cout:http://c.biancheng.net
cerr:http://c.biancheng.net
clog:http://c.biancheng.net

Note that this program does not consider the unique meanings of cerr and clog. It is only used to demonstrate the basic usage of cerr and clog. Readers are not recommended to use this method. In addition, if the std namespace is declared in advance in the program, all std:: can be omitted.

Their usage goes far beyond that. The istream and ostream classes provide many practical functions. Of course, cin, cout, cerr and clog can also be called as class objects.

Table 1 lists some commonly used member methods of the cin object and their functions:

Table 2 lists some commonly used member methods of cout, cerr and clog objects and their functions:

    #include <iostream>
    using namespace std;
    int main() {
        char url[30] = {0};
        //读取一行字符串
        cin.getline(url, 30);
        //输出上一条语句读取字符串的个数
        cout << "读取了 "<<cin.gcount()<<" 个字符" << endl;
        //输出 url 数组存储的字符串
        cout.write(url, 30);
        return 0;
    }

The program execution result is:

http://c.biancheng.net read 23 characters http://c.biancheng.net

Note that Table 1 and Table 2 only list some commonly used member methods in the istream and ostream classes. The specific usage of these methods will be introduced in detail in subsequent chapters.

Guess you like

Origin blog.csdn.net/shiwei0813/article/details/133149818