what is Io Redirection ?
CLanguage callFprintfTo the specifiedFP (File * FP)Write Data,HoweverPrintfYou do not need to specify anyFile *Why?
Start C ProgramThe call environment is automatically created 3 Items File * (stdin, stdout, stderr) , And associate with the corresponding device, while our Scanf Is used Stdin , Printf Yes Stdout But this association can be changed when the program starts. Io Redirection
Each standard input/output descriptor has its own cache. By writing data to their cache blocks, the program can call the corresponding function to read data. For exampleGetchar ()FromStdinTo read one character from the cache block.
# Include <stdio. h>
IntMain ()
{
Printf ("Hello, world \ n");
Return 0;
}
# Gcc-O hello. c
#./Hello
Hello, world
#./Hello>1. txt
PrintfBy default, a line of text is printed to the screen,But the command redirects the output,PrintfWrite text to a specified file
# Include <stdio. h>
IntMain ()
{
CharC;
While(C = getchar ())! = EOF)
{
Printf ("% C", C );
}
Return 0;
}
# Gcc-O Echo. c
#./Echo
GetcharBy default, the input is read from the keyboard.,Including line breaks
Until the user enters from the keyboardEOF (Linux/UnixYesCTRL + D)
#./Hello<./Echo
Hello, world
RedirectionEchoStandard input,HelloOutput as input,
WhenHelloWhen there is no more output, it will sendEOFToEcho
#./Hello<./Echo>2. txt
EchoSlaveHelloGet the input and relocate the output to a file.