Stream redirection

In this guide, we’ll be showing how to perform stream redirection with examples for both Linux and Windows. MacOS users should follow the Linux example. The command line for Linux and MacOS is relatively similar.

As a reminder, stream redirection is a method of redirecting the input or output streams from or to your program. I.e. we can replace reading the input from the keyboard with reading the input from a premade text file, which would contain the inputs you would normally type out during the runtime of the program. We can also redirect the output from the terminal window to a file.

In this guide, we’ll focus on input stream redirection.

Stream redirection on Linux

This part of the guide should work regardless of the Linux distribution (e.g.  Linux Mint, Ubuntu, Arch, Kubuntu, SUSE, …). It also applies for MacOS.

Preparing the files

For this to work, we need to have 2 files:

  1. Our program that we are testing. The program should ask for input from the keyboard (e.g. using a scanf()  statement). There is no need to alter the program code for this!
    Linux and MacOS don’t have file extensions for programs.
  2. Text file containing our input. This will contain the same thing that we would have normally typed out during the execution of the program. The file must be a simple (ASCII) text file. It can be written using basic text editors such as Geany. MS Word, Libreoffice and other complex editors cannot be used.
    In Linux and Mac don’t have file extensions for text files either, but you can use .txt  for clarity.

Most likely we will also have a third file in that folder, which would be the source code for our program. The compilation of this file results in the program that we can execute. Code files have a .c  extension.

You may also notice a fourth file with a .o  extension plus some additional files for multiple input tests or code files.

Create the necessary files

NB! All files must be in the same directory!

First lets create a a text file with our input. I recommend using Geany or any other lightweight text editor for this. Let’s name it input.txt .

The second file will be our source code. The program will read a matrix and print them out nicely aligned. Lets name it matrix_print.c .

Compiling the program

Once the source is ready, let’s compile the program.

NB! After each time you edit the source, you must recompile the code! This tends to be forgotten from time to time when redirection is used. Just saving the file is not enough!

Executing the program using stream redirection

The program must be executed from the command line. Do not execute your program through your IDE (e.g. Geany).

Open the terminal

The simplest way to open the terminal is to first open the file browser and direct it to the folder where you stored all your files. Once there, right-click your mouse on empty space (do not click on a file!) and select “Open terminal”.

This is the preferred method since you will immediately have the right folder active in the terminal.

NB! If your file browser does not support opening the terminal directly, just open the terminal program separately. Once open, navigate it to the correct directory.

To navigate, use the command cd. By writing cd .. , you will navigate to the parent directory. By writing cd folder_name , you will go to the named subdirectory. Most terminals also support full path as the chosen directory, e.g.  cd /home/risto/Desktop/stream_demo . The location should be replaced with the path on your computer. By using the ls  command, you can see what is inside of the current directory.

Check the directory and files

If you wish to make sure that everything is where it should be and your working directory is correct, you can use the command ls . Moreover, if we add the flag -l  (lower case L), we can see the details – e.g. by writing ls -l .

Notice the following

  1. My current working directory is  ~/Desktop/stream_demo , which is also where I saved all of the files.
  2. I have 3 file in this folder. My simple text based input file  ( input.txt ), my program ( matrix_read ) and my source code ( matrix_read.c )
Executing the program

To execute a program (without redirection), we write  ./program , where program  gets replaced with the actual name of your program. Prefix ./  denotes that the program should be executed from the same folder as current working directory (dot symbolizes the current folder).

For my example, it would be  ./matrix_read .

To execute the program while redirecting the input stream, the structure would change to  ./program < input_data_file .

For my example, it would be  ./matrix_read < input.txt .

The following screenshot has been cut into two columns for space saving.

NB! If you execute the program using input stream redirection, you will no longer see the inputs in the terminal window.

Common mistake! The source code of the program cannot be executed, only the program that was compiled from it can. Do not write  ./matrix_read.c . 

Stream redirection in Windows

Heads up! There can be variations depending on the version of Windows. The variations can even exist within one major version (e.g. Things look differently in Windows 10 depending on when the last update was performed).

The guide is based on he classical command prompt.

Preparing the files

For this to work, we need to have 2 files:

  1. Our program that we are testing. The program should ask for input from the keyboard (e.g. using a scanf()  statement). There is no need to alter the program code for this!
    Windows uses a .exe  extension for executable programs.
  2. Text file containing our input. This will contain the same thing that we would have normally typed out during the execution of the program. The file must be a simple (ASCII) text file. It can be written using basic text editors such as Geany, notepad. MS Word, Libreoffice and other complex editors cannot be used.
    Windows uses a .txt extension for text files.

Most likely we will also have a third file in that folder, which would be the source code for our program. The compilation of this file results in the program that we can execute. Code files have a .c  extension.

You may also notice a fourth file with a .o  extension plus some additional files for multiple input tests or code files.

Create the necessary files

NB! All files must be in the same directory!

First lets create a a text file with our input. I recommend using Geany or any other lightweight text editor for this. Let’s name it input.txt .

The second file will be our source code. The program will read a matrix and print them out nicely aligned. Lets name it matrix_print.c .

Compiling the program

Once the source is ready, let’s compile the program.

NB! After each time you edit the source, you must recompile the code! This tends to be forgotten from time to time when redirection is used. Just saving the file is not enough!

Executing the program using stream redirection

The program must be executed from the command line. Do not execute your program through your IDE (e.g. Geany).

Open the terminal

The simplest way to open the terminal is to first open the file browser and direct it to the folder where you stored all your files. Once there, go to the address bar, type cmd  and press enter.

This is the preferred method since you will immediately have the right folder active in the terminal.

Check the directory and files

If you wish to make sure that everything is where it should be and your working directory is correct, you can use the command dirIf you chose to use PoewrShell instead of the classical Command Prompt, the command is called ls  (just as in Linux).

Notice the following

  1. My current working directory is  C:\Users\Risto\Desktop\vood_naide , which is also where I saved all of the files.
  2. I have 3 file in this folder. My simple text based input file  ( input.txt ), my program ( matrix_read.exe ) and my source code ( matrix_read.c )
Executing the program

To execute a program (without redirection), we write  program.exe , where program.exe  gets replaced with the actual name of your program.

For my example, it would be  matrix_read.exe .

To execute the program while redirecting the input stream, the structure would change to  program.exe < input_data_file .

For my example, it would be  ./matrix_read.exe < input.txt .

The following screenshot has been cut into two columns for space saving.

NB! If you execute the program using input stream redirection, you will no longer see the inputs in the terminal window.

Common mistake! The source code of the program cannot be executed, only the program that was compiled from it can. Do not write  ./matrix_read.c . 

Workflow recommendations

When creating programs, you will run into a lot of edit-compile-execute cycles. Some recommendations to optimize your work

  • Do not close the terminal window after testing. Leave it open. This way you don’t need to reopen later.
  • To retest the same program (after changes), press the up arrow    on the keyboard. This will list the previous command you executed from history. You can use arrow keys to traverse the command history.
  • Don’t forget to recompile after making edits.
  • To compile a program using the command line, write
    gcc -o matrix_read -Wall -Wextra -Wconversion matrix_read.c  . This will compile matrix_read.c  into a program called matrix_read . By using arrow keys, you can quickly switch between compile and execute. When on Windows, add the .exe  extension, e.g.   gcc -o matrix_read.exe -Wall -Wextra -Wconversion matrix_read.c

More tricks

  • Instead of writing the program or directory name in full, write the first 1 – 3 characters (sometimes more) and hit the tab  key. This will autocomplete the name for you. If there are multiple files with the same name, you will either toggle between them (Windows) or see a list of possible endings (Linux, Mac). Saves you time and typos.
  • To test your program with different inputs, create multiple text files. E.g. to test bubble sort, create one with random order, one with redundant numbers, one in descending order, one in already sorted order etc. This way you can test your programs quickly using different inputs.

Additional reading