In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of input stream and output stream in C++. What is introduced in this article is very detailed and has certain reference value. Interested friends must finish reading it!
1. Brief introduction
C++ can also be called "C with classes", which can be understood as C++ is a C language based on the addition of object-oriented (classes and objects). On this basis, readers who have learned the C language should know that it has a complete set of solutions for data reading and writing.
Use scanf (), gets () and other functions to read data from the keyboard, use printf (), puts () and other functions to output data to the screen
Use fscanf (), fgets () and other functions to read the data in the file, and use fprintf (), fputs () and other functions to write data to the file.
You know, this set of iUnip O solutions in C language is also suitable for C++ programs, but C++ is not lazy. It has developed a new set of iUniver O solutions on its own, including the cin and cout that everyone has been using. In the previous section, we have been using cin to receive data entered from the keyboard and cout to output data to the screen (these two processes are collectively referred to as "standard iPot O"). In addition, C++ also supports reading data from files and writing data to files (collectively referred to as "file Imango O").
In essence, C++ 's iPlus O solution is a library of classes (as part of the C++ standard library), often referred to as "streams".
C++ developers believe that the process of data input and output is also a process of data transmission, and data flows from one place to another like water, so C++ calls this process "flow". The class that implements this process is called "stream".
The following figure shows these stream classes used to implement data input and output in C++ and the relationship between them:
Stream classes in the C++ class library:
Where the arrows in the figure represent the derived relationships between the classes. For example, ios is the base class for all stream classes, and it derives istream and ostream. In particular, to avoid the ambiguity of multiple inheritance, both istream and ostream are derived from ios using the virtual keyword (virtual inheritance).
The respective functions of these flow classes in the figure above are:
Istream: commonly used to receive data entered from the keyboard
Ostream: often used to output data to the screen
Ifstream: used to read data in a file
Ofstream: used to write data to a file
Iostream: inherits from the istream and ostream classes, because this class has both functions and can be used for both input and output
Fstream: both ifstream and ofstream functions in one, which can not only read data in the file, but also write data to the file.
This chapter only explains the istream, ostream, and iostream classes that implement the standard Imacuro operation, and the stream classes that implement the file Icano operation are explained in the following chapters.
2. C++ input stream and output stream
In the previous chapter, when it comes to inputting or outputting data, we immediately think of cin and cout. In fact, cin is the object of the istream class, and cout is the object of the ostream class, and they are all declared in the header file, which explains why "cin and cout can be used when introduced in C++ programs" (using cin and cout, of course, you also need to declare the std namespace).
In addition, two ostream class objects, cerr and clog, are declared in the header file. 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 visible only to program developers and does not need to be disclosed to ordinary users).
The differences between cout, cerr, and clog are as follows:
In addition to outputting data to the screen, cout can also output data to a specified file through redirection (which will be discussed later). Neither cerr nor clog supports redirection, they can only output data to the screen.
Both cout and clog have buffers, that is, when they output data, they will first put the data into the buffer, and wait until the buffer is full or manually wrap (using the newline character'\ n' or endl) before all the data is displayed on the screen, while cerr has no buffer and outputs the data directly to the screen.
Apart from the above two feature differences, there is no difference between cerr, clog, and cout. The reason why we often use cout is that cerr and clog have different applicable scenarios. In the case of cerr, once the program uses cerr somewhere to output data, we will naturally assume that the output here is a warning or error message.
It is worth mentioning that, like cin, cout, cerr and clog, they were 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 header file also declares four built-in objects that handle wide characters, namely wcin, wcout, wcerr, and wclog, which are not the focus of this section and will not be explained in detail here.
The following program demonstrates the basic usage of cin, cout, cerr, and clog:
# include # include int main () {std::string url; std::cin > > url; std::cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.