Talking about standard input and output stream

body content

                 1. Redirection of standard input and output

                 2. Implementation of standard input and output redirection in the command line

                 3. Some problems with standard input and output redirection

foreword

                  From the moment we enter the world of programming, our work is inseparable from standard input and output. In the windows system, we generally perform various tasks by entering commands in the command prompt, that is, cmd.exe. We can enter the dir command to get the current file directory information, and the file information will be immediately echoed in the cmd.exe console. If we want to get directory information again, we need to repeatedly enter the dir command on the command line again.

1. Redirection of standard input and output

             1. What is standard input and output redirection 

                 The standard input stdin  takes cmd.exe as an example. When we perform certain tasks, we need to interact with the console. This interaction process requires us to perform a series of inputs in the console to respond to the execution of the program. At this time, this The input process can be considered as standard input. At this time, for the process, it is the user who completes the input, that is, the user of the computer.

                 Standard input, stdout,   and standard output are relative to standard input. After we input instructions in the console, the computer program may have some feedback on our input, and it will print out the response information on the console.

                 Standard error stderr  standard error is similar to standard output, which contains the output of some error messages processed by the program, and can be combined with standard output

                 The process of input and output can be compared to two acquaintances greeting each other in a question-and-answer format. A says Hello to B! B may answer Hi, A. Then A and B complete the process of input and output. For B, A is the input, and for A, B is the output.

                 For computers, information exists in the form of data. The process of interaction between A and B above is the process of information exchange. Above, A and B greet each other through language and complete the exchange of information in the form of voice. So how do A and B greet each other without speaking? Obviously, this process can be completed in other forms such as passing notes, patting the shoulders, and making eye contact.

                 Input and output redirection is roughly similar to this process. First of all, it does not change the function of the program, but only changes the way or carrier of information transmission. The input of the program is changed from the console input to the input from the file, and the output of the program is changed from the echo of the console to the echo of the file.

             2. The benefits of standard input and output redirection

                 So what are the benefits of this process? The obvious benefit is to reduce a lot of repetitive and cumbersome input and ensure the accuracy of the input. After all, when there is a lot of input, the human input error rate is very high. The output to the file realizes the persistent storage of the information and facilitates the utilization of the information by other programs. For example, when analyzing program running logs for troubleshooting. The entire redirection process has changed from requiring human interaction to automatic completion, which reduces the time for human participation.

              

Second, the implementation of standard input and output redirection in the command line

           1. Standard input is realized through the < symbol

               Take the sort command as an example, before the standard input, we need to enter a certain amount of data in the command line to sort the data

              

                As shown in the figure above, we need to input n lines of data and use ctrl+z to end the input to complete the sorting process. We can enter this input data into a file and implement reading and writing from the file. 

It should be noted that when using start to start a new process and process the input redirection of the new process, you need to use ^ < instead of <

Of course, in this case the output also refers to the input 

      

       2. The standard input is realized through the pipe character | 

              Still taking the above sort command as an example, it can read input from a file, and it can also take the output of other processes as its input. From the perspective of narrow storage, files represent disk storage, and the output of other processes represents memory storage. The redirection input is actually for the data itself.

             The type command can print the information of the file under the windows operating system

              

              At this time, the type command combined with the pipe symbol | can realize the sort command to accept the output from the process as its input

              

      3. Standard output is implemented using the > symbol

                

       4. Standard input and output combined with simultaneous use

                   

        5. The standard error stream can be implemented using 2 >&1

             The standard error stream is similar to the standard output stream and is often used in conjunction with the standard output stream.

3. Some problems with standard input and output redirection

        Since there is a certain inheritance relationship between the parent and child processes, the child process inherits the standard input and output streams of the parent process by default. Taking the java program as an example, the java process uses the ProcessBuilder class to start a new process

        

          Process output can be read through proc.getInputStream(), and process input can be written through proc.getOutputStream. , if processBuilder specifies inheritIO(), it will not be able to read any data from proc.getInputStream(), nor can it write any data from proc.getOutputStream.

           And whether the output of the child process can be read in real time depends on whether the child process calls flush to refresh the output buffer at an appropriate time.

       

       

         

                  

            

                

              

                   

               

                  

                  

Guess you like

Origin blog.csdn.net/weixin_38526093/article/details/130187820