C + + Learning 42 concepts of input and output

Source: Internet
Author: User

We often use the input and output, are the object of the terminal, that is, from the keyboard input data, running results output to the monitor screen. From the operating system's point of view, every input connected to a host is considered a file. In addition to the terminal for the object input and output, but also often use Disk (CD) as input and output objects, disk files can be both as input files, but also as output files.

The input of the program refers to the transfer of data from the input file to the program, the output of the program refers to the transfer of data from the program to the output file.

The C + + input and output contains the following three aspects:

    1. The input and output of standard devices specified by the system. That is, input data from the keyboard, output to the monitor screen. This input and output is referred to as standard input and output, referred to as standard I/O.
    2. The external memory disk file is the input and output of the object, that is, the input data from the disk file, the data output to the disk file. The input output of the external memory file is called the input and output of the file, referred to as file I/O.
    3. Inputs and outputs the space specified in memory. You typically specify a character array as storage space (you can actually use that space to store any information). This input and output is called string input and output, referred to as String I/O.

C + + takes different methods to implement the above input and output. In order to realize the effective flow of data, C + + system provides a large I/O class library, calling different classes to implement different functions.

In C language, the input and output of printf and scanf are not always guaranteed to be reliable and safe in the input and output data. In the input and output of C + +, the compiling system strictly checks the data type, and it is impossible to compile any type of incorrect data. Therefore, the I/O operations of C + + are type safe. The I/O operations of C + + are extensible and can be used not only to enter data for the output standard type, but also for user-defined types of data. C + + uses the same approach for data of standard types and for input and output of user-declared type data. C + + uses the I/O class library to achieve rich I/O functionality. The input and output of C + + is better than printf and scanf in C language, but it is more complicated and has many details.

C + + I/O development-type safety and scalability

In C language, using PRIMF and scanf for output, often can not guarantee that the input and output data is reliable and safe. Readers who have studied C can analyze the following usage, want to output an integer with Format delimiter%d, but accidentally output a single-precision variable and a string, what will happen? Assume that the system int used is two bytes.
printf ("%d", I); I is integer variable, correct, output I value
printf ("%d", f); F is a single-precision variable, outputting the contents of the first two bytes in the f variable
printf ("%d", "C + +"); Address of the output Yu string "C + +"

The compilation system considers the above statements to be legal, and does not check the legality of the data type, obviously the result is not what people expect, when using scanf input, sometimes the problem is very concealed. Such as:
scanf ("%d", &i); Correct, enter an integer, assign to integer variable i
scanf ("%d", I); Missing Write &

If there is already a declaration statement "int i = 1; ", define I as an integer variable with an initial value of 1. The compilation system does not consider the above scanf statement to be an error, but instead stores the value of the sender in a memory unit with an address of 000001, which can have serious consequences.

C + + in order to be compatible with C, the use of printf and scanf to the output and the method of human, so that the large number of C programs written in the past can still run in the environment of C + +, but I hope readers in the new C + + program will not use the C input and output mechanism, and to use C + + Their own unique output method of loss. In the input and output of C + +, the compiling system strictly checks the data type, and it is impossible to compile any type of incorrect data. Therefore, the I/O operations of C + + are type safe.

In addition, you can output and input standard types of data (such as int, float, double, char) with printf and scanf, but you cannot output data for types that the user declares (such as arrays, structs, classes). In C + +, the output of the input to the class object is often encountered and obviously cannot be handled using printf and scanf. The I/O operations of C + + are extensible and can be used not only for output of standard types of data, but also for user-defined types of data. C + + is handled in the same way for standard type of data and output of user-declared type data. Obviously, after the user declares a new class, it is impossible to output and lose the object of this class directly with printf and the scanf function.

Extensibility is one of the important features of C + + output, it can improve the reusability of software and speed up the process of software development.

C + + uses the I/O class library to achieve rich I/O functionality. This makes C + + output significantly better than the C language of printf and scanf, but also paid the price, C + + system becomes more complex, to master many details. In this chapter, we can only introduce its basic concepts and basic operations, and some specific details can be further mastered in the future when practical in-depth application.

C + + Learning 42 concepts of input and output

Related Article

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.