C Input Buffering and redirection

Source: Internet
Author: User

Input buffers:

Let's look at a echo applet, which displays the characters entered by the user on the screen.

Echo.c
#include <stdio.h>int main () { int ch; while ((Ch=getchar ())! =EOF) { Putchar (ch); }}

The result of the operation is as follows (press Ctrl+d to exit the loop, program end)

One possible result of this program on different systems is

Hheelloo

This is generally an exception, and on most systems nothing happens until you press ENTER, as shown in the first result.

An instance of the input character immediately echoes a non-buffered (unbuffered) or direct input, which indicates that the input character is immediately available to the waiting program.

Conversely, the deferred Echo is an instance of the buffered (buffered) input, in which case the characters entered are collected and stored in a temporary storage area called buffer, when the Enter word converts sequential blocks is made available to the program when you press ENTER. So they show up on the screen together.

The role of the buffer:

1. Transferring several characters as a block is less time consuming than sending these characters individually

2. If the input is incorrect, you can use the DELETE key to modify

Applicability of non-buffered inputs: Some interactive programs that want to press the key to execute a command

Buffering can also be divided into two categories:

Fully buffered I/O: buffers are emptied when the buffer is full (content is sent to other destinations), and such buffers usually appear in the file input

Row buffer I/o: empties the buffer when a newline character is encountered, the keyboard is the standard row buffer, and pressing ENTER will empty the buffer

Redirect:

By default, a C program using standard IO packages uses standard input as its input stream, which is established as a normal way to read data to a computer and is now generally our keyboard. The standard output stream is typically our display.

However, we can redirect the standard input and output stream to a file

Now use the Mywords file (a randomly built file, enter some characters) instead of the keyboard as our input using echo

  

The Echo program shows the contents of the mywords.

  The < symbol is a UNIX, Linux, DOS redirection operator that associates the Mywords file with the standard input stream and directs the contents of the file to the Echo program.

Similarly, you can use the > symbol to redirect the output stream to a file

  

You can see that the program is outputting the string we entered from the keyboard to the MyWord, rather than echoing it on the screen.

Reference: C Primer Plus

C Input Buffering and redirection

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.