{"id":7448,"date":"2022-10-02T16:18:40","date_gmt":"2022-10-02T14:18:40","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=7448"},"modified":"2025-06-20T21:11:04","modified_gmt":"2025-06-20T19:11:04","slug":"stream-redirection","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/","title":{"rendered":"Stream redirection"},"content":{"rendered":"<p>In this guide, we&#8217;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.<\/p>\n<p>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.<\/p>\n<p>In this guide, we&#8217;ll focus on input stream redirection.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Stream_redirection_on_Linux\" >Stream redirection on Linux<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Preparing_the_files\" >Preparing the files<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Create_the_necessary_files\" >Create the necessary files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Compiling_the_program\" >Compiling the program<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Executing_the_program_using_stream_redirection\" >Executing the program using stream redirection<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Open_the_terminal\" >Open the terminal<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Check_the_directory_and_files\" >Check the directory and files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Executing_the_program\" >Executing the program<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Stream_redirection_in_Windows\" >Stream redirection in Windows<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Preparing_the_files-2\" >Preparing the files<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Create_the_necessary_files-2\" >Create the necessary files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Compiling_the_program-2\" >Compiling the program<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Executing_the_program_using_stream_redirection-2\" >Executing the program using stream redirection<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Open_the_terminal-2\" >Open the terminal<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Check_the_directory_and_files-2\" >Check the directory and files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Executing_the_program-2\" >Executing the program<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Workflow_recommendations\" >Workflow recommendations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#More_tricks\" >More tricks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/#Additional_reading\" >Additional reading<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Stream_redirection_on_Linux\"><\/span>Stream redirection on Linux<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This part of the guide should work regardless of the Linux distribution (e.g.\u00a0 Linux Mint, Ubuntu, Arch, Kubuntu, SUSE, &#8230;). It also applies for MacOS.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Preparing_the_files\"><\/span>Preparing the files<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>For this to work, we need to have 2 files:<\/p>\n<ol>\n<li>Our program that we are testing. The program should ask for input from the keyboard (e.g. using a <span class=\"lang:default highlight:0 decode:true crayon-inline \">scanf()<\/span>\u00a0 statement). There is no need to alter the program code for this!<br \/>\nLinux and MacOS don&#8217;t have file extensions for programs.<\/li>\n<li>Text file containing our input. This will contain the same thing that we would have normally typed out during the execution of the program. <strong>The file must be a simple (ASCII) text file. <\/strong>It can be written using basic text editors such as Geany. MS Word, Libreoffice and other complex editors cannot be used.<br \/>\nIn Linux and Mac don&#8217;t have file extensions for text files either, but you can use <span class=\"lang:default highlight:0 decode:true crayon-inline\">.txt<\/span>\u00a0 for clarity.<\/li>\n<\/ol>\n<p>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 <span class=\"lang:default highlight:0 decode:true crayon-inline \">.c<\/span>\u00a0 extension.<\/p>\n<p>You may also notice a fourth file with a <span class=\"lang:default highlight:0 decode:true crayon-inline \">.o<\/span>\u00a0 extension plus some additional files for multiple input tests or code files.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Create_the_necessary_files\"><\/span>Create the necessary files<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>NB! All files must be in the same directory!<\/p>\n<p>First lets create a a text file with our input. I recommend using Geany or any other lightweight text editor for this. Let&#8217;s name it <span class=\"lang:default highlight:0 decode:true crayon-inline \">input.txt<\/span>\u00a0.<\/p>\n<pre class=\"toolbar:1 lang:c highlight:0 decode:true\" title=\"input.txt\">3 9 -4 0 -10 10 5\r\n1 -4 9 1 -7 -9 4\r\n-5 5 -5 5 -5 5 -5\r\n7 6 5 4 3 2 1\r\n1 2 3 4 5 6 7\r\n-1 -2 -3 -4 -3 -2 -1\r\n0 0 0 0 0 0 0<\/pre>\n<p>The second file will be our source code. The program will read a matrix and print them out nicely aligned. Lets name it <span class=\"lang:default highlight:0 decode:true crayon-inline \">matrix_print.c<\/span>\u00a0.<\/p>\n<pre class=\"lang:c decode:true \" title=\"matrix_print.c\">#include &lt;stdio.h&gt;\r\n\r\n#define ARR_LEN 7\r\n\r\nint main(void)\r\n{\r\n    int matrix[ARR_LEN][ARR_LEN];\r\n\r\n    for (int i = 0; i &lt; ARR_LEN; i++)\r\n    {\r\n        for (int j = 0; j &lt; ARR_LEN; j++)\r\n        {\r\n            printf(\"Enter matrix element [%d][%d]\\n\", i + 1, j + 1);\r\n            scanf(\"%d\", &amp;matrix[i][j]);\r\n        }\r\n    }\r\n\r\n    printf(\"Entered matrix:\\n\");\r\n    for (int i = 0; i &lt; ARR_LEN; i++)\r\n    {\r\n        for (int j = 0; j &lt; ARR_LEN; j++)\r\n        {\r\n            printf(\"%6d\", matrix[i][j]);\r\n        }\r\n        putchar('\\n');\r\n    }\r\n    return 0;\r\n}<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"Compiling_the_program\"><\/span>Compiling the program<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Once the source is ready, let&#8217;s compile the program.<\/p>\n<p><strong>NB! After each time you edit the source, you must recompile the code!<\/strong> This tends to be forgotten from time to time when redirection is used. Just saving the file is not enough!<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Executing_the_program_using_stream_redirection\"><\/span>Executing the program using stream redirection<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The program must be executed from the command line. Do not execute your program through your IDE (e.g. Geany).<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Open_the_terminal\"><\/span>Open the terminal<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>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 &#8220;Open terminal&#8221;.<\/p>\n<p>This is the preferred method since you will immediately have the right folder active in the terminal.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/1_open_terminal.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7435\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/1_open_terminal.png\" alt=\"\" width=\"755\" height=\"672\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/1_open_terminal.png 755w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/1_open_terminal-300x267.png 300w\" sizes=\"auto, (max-width: 755px) 100vw, 755px\" \/><\/a><\/p>\n<p><strong>NB! If your file browser does not support opening the terminal directly<\/strong>, just open the terminal program separately. Once open, navigate it to the correct directory.<\/p>\n<p>To navigate, use the command cd. By writing <span class=\"lang:default highlight:0 decode:true crayon-inline \">cd ..<\/span>\u00a0, you will navigate to the parent directory. By writing <span class=\"lang:default highlight:0 decode:true crayon-inline \">cd folder_name<\/span> , you will go to the named subdirectory. Most terminals also support full path as the chosen directory, e.g.\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">cd \/home\/risto\/Desktop\/stream_demo<\/span> . The location should be replaced with the path on your computer. By using the <span class=\"lang:default highlight:0 decode:true crayon-inline\">ls<\/span>\u00a0 command, you can see what is inside of the current directory.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Check_the_directory_and_files\"><\/span>Check the directory and files<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>If you wish to make sure that everything is where it should be and your working directory is correct, you can use the command <span class=\"lang:default highlight:0 decode:true crayon-inline \">ls<\/span>\u00a0. Moreover, if we add the flag <span class=\"lang:default highlight:0 decode:true crayon-inline \">-l<\/span>\u00a0 (lower case L), we can see the details &#8211; e.g. by writing <span class=\"lang:default highlight:0 decode:true crayon-inline \">ls -l<\/span>\u00a0.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/2_ls.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7436\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/2_ls.png\" alt=\"\" width=\"512\" height=\"165\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/2_ls.png 512w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/2_ls-300x97.png 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/p>\n<p>Notice the following<\/p>\n<ol>\n<li>My current working directory is\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">~\/Desktop\/stream_demo<\/span> , which is also where I saved all of the files.<\/li>\n<li>I have 3 file in this folder. My simple text based input file\u00a0 (<span class=\"lang:default highlight:0 decode:true crayon-inline\">input.txt<\/span> ), my program (<span class=\"lang:default highlight:0 decode:true crayon-inline \">matrix_read<\/span> ) and my source code (<span class=\"lang:default highlight:0 decode:true crayon-inline \">matrix_read.c<\/span>\u00a0)<\/li>\n<\/ol>\n<h5><span class=\"ez-toc-section\" id=\"Executing_the_program\"><\/span>Executing the program<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>To execute a program (without redirection), we write\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">.\/program<\/span> , where <span class=\"lang:default highlight:0 decode:true crayon-inline\">program<\/span>\u00a0 gets replaced with the actual name of your program. Prefix <span class=\"lang:default highlight:0 decode:true crayon-inline\">.\/<\/span>\u00a0 denotes that the program should be executed from the same folder as current working directory (dot symbolizes the current folder).<\/p>\n<p>For my example, it would be\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">.\/matrix_read<\/span>\u00a0.<\/p>\n<p>To execute the program while redirecting the input stream, the structure would change to\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">.\/program &lt; input_data_file<\/span> .<\/p>\n<p>For my example, it would be\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">.\/matrix_read &lt; input.txt<\/span>\u00a0.<\/p>\n<p>The following screenshot has been cut into two columns for space saving.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_execute.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7439\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_execute.png\" alt=\"\" width=\"916\" height=\"508\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_execute.png 916w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_execute-300x166.png 300w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_execute-768x426.png 768w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_execute-672x372.png 672w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><\/a><\/p>\n<p>NB! If you execute the program using input stream redirection, you will no longer see the inputs in the terminal window.<\/p>\n<p><span style=\"color: #ff0000;\">Common mistake! The source code of the program cannot be executed, only the program that was compiled from it can. <strong>Do not write<\/strong>\u00a0<\/span><span style=\"color: #ff0000;\"> <span class=\"lang:default highlight:0 decode:true crayon-inline\">.\/matrix_read.c<\/span>\u00a0.\u00a0<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Stream_redirection_in_Windows\"><\/span>Stream redirection in Windows<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>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).<\/p>\n<p>The guide is based on he classical command prompt.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Preparing_the_files-2\"><\/span>Preparing the files<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>For this to work, we need to have 2 files:<\/p>\n<ol>\n<li>Our program that we are testing. The program should ask for input from the keyboard (e.g. using a <span class=\"lang:default highlight:0 decode:true crayon-inline \">scanf()<\/span>\u00a0 statement). There is no need to alter the program code for this!<br \/>\nWindows uses a <span class=\"lang:default highlight:0 decode:true crayon-inline \">.exe<\/span>\u00a0 extension for executable programs.<\/li>\n<li>Text file containing our input. This will contain the same thing that we would have normally typed out during the execution of the program. <strong>The file must be a simple (ASCII) text file. <\/strong>It can be written using basic text editors such as Geany, notepad. MS Word, Libreoffice and other complex editors cannot be used.<br \/>\nWindows uses a .txt extension for text files.<\/li>\n<\/ol>\n<p>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 <span class=\"lang:default highlight:0 decode:true crayon-inline \">.c<\/span>\u00a0 extension.<\/p>\n<p>You may also notice a fourth file with a <span class=\"lang:default highlight:0 decode:true crayon-inline \">.o<\/span>\u00a0 extension plus some additional files for multiple input tests or code files.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Create_the_necessary_files-2\"><\/span>Create the necessary files<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>NB! All files must be in the same directory!<\/p>\n<p>First lets create a a text file with our input. I recommend using Geany or any other lightweight text editor for this. Let&#8217;s name it <span class=\"lang:default highlight:0 decode:true crayon-inline \">input.txt<\/span>\u00a0.<\/p>\n<pre class=\"toolbar:1 lang:c highlight:0 decode:true\" title=\"input.txt\">3 9 -4 0 -10 10 5\r\n1 -4 9 1 -7 -9 4\r\n-5 5 -5 5 -5 5 -5\r\n7 6 5 4 3 2 1\r\n1 2 3 4 5 6 7\r\n-1 -2 -3 -4 -3 -2 -1\r\n0 0 0 0 0 0 0<\/pre>\n<p>The second file will be our source code. The program will read a matrix and print them out nicely aligned. Lets name it <span class=\"lang:default highlight:0 decode:true crayon-inline \">matrix_print.c<\/span>\u00a0.<\/p>\n<pre class=\"lang:c decode:true\" title=\"matrix_print.c\">#include &lt;stdio.h&gt;\r\n\r\n#define ARR_LEN 7\r\n\r\nint main(void)\r\n{\r\n    int matrix[ARR_LEN][ARR_LEN], i, j;\r\n    for (i = 0; i &lt; ARR_LEN; i++)\r\n    {\r\n        for (j = 0; j &lt; ARR_LEN; j++)\r\n        {\r\n            printf(\"Enter matrix element [%d][%d]\\n\", i + 1, j + 1);\r\n            scanf(\"%d\", &amp;matrix[i][j]);\r\n        }\r\n    }\r\n    printf(\"Entered matrix:\\n\");\r\n    for (i = 0; i &lt; ARR_LEN; i++)\r\n    {\r\n        for (j = 0; j &lt; ARR_LEN; j++)\r\n        {\r\n            printf(\"%6d\", matrix[i][j]);\r\n        }\r\n        putchar('\\n');\r\n    }\r\n    return 0;\r\n}<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"Compiling_the_program-2\"><\/span>Compiling the program<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Once the source is ready, let&#8217;s compile the program.<\/p>\n<p><strong>NB! After each time you edit the source, you must recompile the code!<\/strong> This tends to be forgotten from time to time when redirection is used. Just saving the file is not enough!<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Executing_the_program_using_stream_redirection-2\"><\/span>Executing the program using stream redirection<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The program must be executed from the command line. Do not execute your program through your IDE (e.g. Geany).<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Open_the_terminal-2\"><\/span>Open the terminal<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>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 <span class=\"lang:default highlight:0 decode:true crayon-inline \">cmd<\/span>\u00a0 and press enter.<\/p>\n<p>This is the preferred method since you will immediately have the right folder active in the terminal.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/1_win_open.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7441\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/1_win_open.png\" alt=\"\" width=\"811\" height=\"305\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/1_win_open.png 811w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/1_win_open-300x113.png 300w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/1_win_open-768x289.png 768w\" sizes=\"auto, (max-width: 811px) 100vw, 811px\" \/><\/a><\/p>\n<h5><span class=\"ez-toc-section\" id=\"Check_the_directory_and_files-2\"><\/span>Check the directory and files<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>If you wish to make sure that everything is where it should be and your working directory is correct, you can use the command <span class=\"lang:default highlight:0 decode:true crayon-inline\">dir<\/span> .\u00a0<em>If you chose to use PoewrShell instead of the classical Command Prompt, the command is called <span class=\"lang:default highlight:0 decode:true crayon-inline \">ls<\/span>\u00a0 (just as in Linux).<a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/2_win_dir.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7442\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/2_win_dir.png\" alt=\"\" width=\"433\" height=\"318\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/2_win_dir.png 433w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/2_win_dir-300x220.png 300w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/a><\/em><\/p>\n<p>Notice the following<\/p>\n<ol>\n<li>My current working directory is\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">C:\\Users\\Risto\\Desktop\\vood_naide<\/span>\u00a0, which is also where I saved all of the files.<\/li>\n<li>I have 3 file in this folder. My simple text based input file\u00a0 (<span class=\"lang:default highlight:0 decode:true crayon-inline\">input.txt<\/span> ), my program (<span class=\"lang:default highlight:0 decode:true crayon-inline\">matrix_read.exe<\/span> ) and my source code (<span class=\"lang:default highlight:0 decode:true crayon-inline \">matrix_read.c<\/span>\u00a0)<\/li>\n<\/ol>\n<h5><span class=\"ez-toc-section\" id=\"Executing_the_program-2\"><\/span>Executing the program<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>To execute a program (without redirection), we write\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">program.exe<\/span> , where <span class=\"lang:default highlight:0 decode:true crayon-inline\">program.exe<\/span>\u00a0 gets replaced with the actual name of your program.<\/p>\n<p>For my example, it would be\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">matrix_read.exe<\/span>\u00a0.<\/p>\n<p>To execute the program while redirecting the input stream, the structure would change to\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">program.exe &lt; input_data_file<\/span> .<\/p>\n<p>For my example, it would be\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">.\/matrix_read.exe &lt; input.txt<\/span>\u00a0.<\/p>\n<p>The following screenshot has been cut into two columns for space saving.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_exec_win.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7443\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_exec_win.png\" alt=\"\" width=\"909\" height=\"496\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_exec_win.png 909w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_exec_win-300x164.png 300w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/10\/3_exec_win-768x419.png 768w\" sizes=\"auto, (max-width: 909px) 100vw, 909px\" \/><\/a><\/p>\n<p>NB! If you execute the program using input stream redirection, you will no longer see the inputs in the terminal window.<\/p>\n<p><span style=\"color: #ff0000;\">Common mistake! The source code of the program cannot be executed, only the program that was compiled from it can. <strong>Do not write<\/strong>\u00a0<\/span><span style=\"color: #ff0000;\"> <span class=\"lang:default highlight:0 decode:true crayon-inline\">.\/matrix_read.c<\/span>\u00a0.\u00a0<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Workflow_recommendations\"><\/span>Workflow recommendations<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>When creating programs, you will run into a lot of edit-compile-execute cycles. Some recommendations to optimize your work<\/p>\n<ul>\n<li>Do not close the terminal window after testing. Leave it open. This way you don&#8217;t need to reopen later.<\/li>\n<li>To retest the same program (after changes), press the up arrow\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">\u2191<\/span>\u00a0 on the keyboard. This will list the previous command you executed from history. You can use arrow keys to traverse the command history.<\/li>\n<li>Don&#8217;t forget to recompile after making edits.<\/li>\n<li>To compile a program using the command line, write<br \/>\n<span class=\"lang:default highlight:0 decode:true crayon-inline\">gcc -o matrix_read -Wall -Wextra -Wconversion matrix_read.c<\/span>\u00a0 . This will compile <span class=\"lang:default highlight:0 decode:true crayon-inline \">matrix_read.c<\/span>\u00a0 into a program called <span class=\"lang:default highlight:0 decode:true crayon-inline \">matrix_read<\/span>\u00a0. By using arrow keys, you can quickly switch between compile and execute. When on Windows, add the <span class=\"lang:default highlight:0 decode:true crayon-inline \">.exe<\/span>\u00a0 extension, e.g. \u00a0<span class=\"lang:default highlight:0 decode:true crayon-inline\">gcc -o matrix_read.exe -Wall -Wextra -Wconversion matrix_read.c<\/span><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"More_tricks\"><\/span><strong>More tricks<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Instead of writing the program or directory name in full, write the first 1 &#8211; 3 characters (sometimes more) and hit the <span class=\"lang:default highlight:0 decode:true crayon-inline \">tab<\/span>\u00a0 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.<\/li>\n<li>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.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Additional_reading\"><\/span><strong>Additional reading<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Standard_streams\">https:\/\/en.wikipedia.org\/wiki\/Standard_streams<\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Redirection_(computing)\">https:\/\/en.wikipedia.org\/wiki\/Redirection_(computing)<\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Pipeline_(Unix)\">https:\/\/en.wikipedia.org\/wiki\/Pipeline_(Unix)<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we&#8217;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. &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/stream-redirection\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Stream redirection<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4802,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width.php","meta":{"footnotes":""},"class_list":["post-7448","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/7448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=7448"}],"version-history":[{"count":2,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/7448\/revisions"}],"predecessor-version":[{"id":10259,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/7448\/revisions\/10259"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/4802"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=7448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}