GIDNetwork > Piping and Redirection
Register
« Colonial Marines RPG has moved! What are Google Sitemaps good for? »

Piping and Redirection

by: WaltP - Aug 23, 2005

There are three predefined devices:

  • stdin -- standard input

  • stdout -- standard output

  • stderr -- standard error

stdin is generally assigned to the keyboard, stdout & stderr are assigned to the screen. The important thing about these devices is their flexibility -- and "pipe"-ability.

A "pipe" is a technique for receiving input and output from alternative sources. An excellent example is the program sort.exe in the Windows system. Similar techniques are used on Linux and Unix.

From a console window, if you run sort, the program will wait for input.

  1. Type in a few lines of text.

  2. Follow the text with Ctrl-Z and Enter.

Sort will then display these lines in sorted order. Input was from stdin, output to stdout.

Now, lets take the program one step further. Three characters are used in relation to pipes. These are:

> -- input from file
< -- output to file
| -- move output to... (explained below)

Now run sort with the following command:

Generic Code Example:

sort <filein

replacing filein with the name of a text file. You will see the contents of filein displayed to the screen with the lines sorted. The < asked the operating system to pass the contents of the file to sort via stdin.

In reverse, use the command:

Generic Code Example:

sort >fileout

replacing fileout with a nonexistent file. Enter lines from the keyboard, Ctrl-Z/Enter. The sorted output will now be found in fileout. The operating system moved the output to the file designated by the pipe character >.

One step further:

Generic Code Example:

sort >fileout <filein

reads input from filein and sends output to fileout. The order of the files is immaterial, and only one < and > are allowed.

The third pipe character | will take the output from one program and use it as input for another. For example:

Generic Code Example:

1) type filename | sort
2) type filename | sort >fileout

type will display the contents of the file to stdout. That output will be used as the input to sort and the file contents will be sorted and (1) displayed or (2) output to fileout.

more is another program that works similarly. It will take the input it receives and display it on the screen in pages.

Generic Code Example:

type filename | more

will display the file, one screenful at a time. Hitting the Spacebar will display the next screen, Enter will add the next line to the display. These programs (called filters) can be chained together:

Generic Code Example:

type filename | sort | more

So, if you write programs that deal with stdin and stdout, you too can pipe information into and out of the program.

Let's say you wish to do this with your program. You have errors coded like:

C/CPP/C++ Code Example:

if (error == 2)
{
    printf("The data did not contain a value\n");
    return(error);
}
else
if (error == 5)
{
    printf("A premature end of data was encountered.\n");
    printf("Check your input.\n");
    return(error);
}

If you pipe your output to a file, the errors will get added to the file. Let's assume you still want the errors to go to the screen instead. Simply output the errors to stderr:

C/CPP/C++ Code Example:

if (error == 2)
{
    fprintf(stderr, "The data did not contain a value\n");
    return(error);
}
else
if (error == 5)
{
    fprintf(stderr, "A premature end of data was encountered.");
    fprintf(stderr, "Check your input.\n");
    return(error);
}

No muss, no fuss. Data to the file; errors (and anything else you need) to the screen.

Some things you may wish to send to stderr:

  1. Errors

  2. Version Number

  3. Program Title

  4. Program Progress Information

In other words, anything to help the user but is not essential to the output file.

Would you like to comment? This story has been viewed 10,167 times.
« Colonial Marines RPG has moved! What are Google Sitemaps good for? »

__top__

Copyright © GIDNetwork™ 2001 - 2017

Another website by J de Silva

Page generated in : 0.01214 sec.