User Tools

Site Tools


guides:software:redirection

I/O Redirection (<>)

I/O redirection is part of most shell systems. This process allows the user to enter data into a program from a file instead of the keyboard, or send the output of a program to a file instead of the screen.

Quick Reference

To redirect the standard input to the program hello from the file queenName:

$ hello < queenName

To redirect the standard output of the program hello to the file outfile

$ hello > outfile

An Example

An example might be a good place to start. Assume the following code is in hello.cpp

#include <iostream>
 
using namespace std;
 
int main() {
   string name;
   cout << "Enter your name : ";
   getline(cin, name);
   cout << endl;
   cout << "Hello " << name << "!" << endl;
 
   return 0;
}

When this code is compiled and run, the output will be something like this

$ hello
Enter your name : Dan

Hello Dan!

In this example the input is read from the keyboard and the output is written to the screen.

But let's assume that you want to test your program for Queen Elizabeth when she is in Scotland. Her official style is Elizabeth the Second, by the Grace of God of the United Kingdom of Great Britain and Northern Ireland and of Her other Realms and Territories Queen, Head of the Commonwealth, Defender of the Faith , which is just a little too much to type every time you wish to test your program.

A solution is to place this information in a file. So create the file queenName that contains this information.

$ cat queenName
Elizabeth the Second, by the Grace of God of the United Kingdom of Great Britain and Northern Ireland and of Her other Realms and Territories Queen, Head of the Commonwealth, Defender of the Faith

Now the program can be run using file redirection behaving as if the user entered the Queen's style.

$ hello < queenName 
Enter your name : 
Hello Elizabeth the Second, by the Grace of God of the United Kingdom of Great Britain and Northern Ireland and of Her other Realms and Territories Queen, Head of the Commonwealth, Defender of the Faith!

Notice in this example, the input from the file does not show as if it had been typed.

Output redirection can be used to save the standard output of a program to a file.

$ hello > outfile
Dan

$ cat outfile
Enter your name : 
Hello Dan!

In this example

  • The user runs the program, redirecting output to the file outfile .
    • Because the prompt was written to the output file, it does not appear on the screen.
    • The user types in a name Dan .
  • The user than displays the contents of the redirected output by typing cat outfile .

Output redirection can be done in both ways, so the following is a valid command.

$ hello < queenName  > outfile
[dbennett@cslab103 tmp]$ cat outfile
Enter your name : 
Hello Elizabeth the Second, by the Grace of God of the United Kingdom of Great Britain and Northern Ireland and of Her other Realms and Territories Queen, Head of the Commonwealth, Defender of the Faith!
guides/software/redirection.txt · Last modified: 2022/08/02 11:59 by 127.0.0.1