C language: Io redirection

Source: Internet
Author: User

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.

 

 


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.