Page 1 of 1

I/O Redirect

#1 MentalFloss   User is offline

  • .
  • member icon

Reputation: 584
  • View blog
  • Posts: 1,528
  • Joined: 02-September 09

Posted 13 September 2018 - 05:14 PM

One technique that I would have really enjoyed knowing about when starting out is that of I/O redirection. So, hopefully this guide will give you that edge, and assist you in your development process. The intended audience is text-based programmers.

The Problem

In developing and testing software, any required inputs manually entered slows down the development process.

The Fix

Use I/O redirection. This is achieved via the command prompt with the following format:

my_program.exe < input_file > output_file


Example

Problem: You are writing a guess the number game in C++.

Implementation:

#include <iostream>

using namespace std;

int main()
{
	int answer = 14;
	int guess = -1;
	while(guess != answer)
	{
		cout << "Enter guess: ";
		cin >> guess;
		
		if(guess == answer)
		{
			cout << "You win! The number was " << answer << endl;
		}
		else
		{
			cout << "You incorrectly guessed " << guess << endl;
		}
	}
}



An example run:

Enter guess: 1
You incorrectly guessed 1
Enter guess: 2
You incorrectly guessed 2
Enter guess: 3
You incorrectly guessed 3
Enter guess: 4
You incorrectly guessed 4
Enter guess: 5
You incorrectly guessed 5
Enter guess: 6
You incorrectly guessed 6
Enter guess: 7
You incorrectly guessed 7
Enter guess: 8
You incorrectly guessed 8
Enter guess: 9
You incorrectly guessed 9
Enter guess: 0
You incorrectly guessed 0
Enter guess: 10
You incorrectly guessed 10
Enter guess: 14
You win! The number was 14



Look at all that wasted time typing out numbers. If this was a harder problem, how much time would it take to go through the debugging?

Let's create a text file of our guesses.

1
4
6
4
15
67
23
11
14
65
76
100



We then run our program like so:

guess.exe < guesses.txt



And here is the output:

Enter guess: You incorrectly guessed 1
Enter guess: You incorrectly guessed 4
Enter guess: You incorrectly guessed 6
Enter guess: You incorrectly guessed 4
Enter guess: You incorrectly guessed 15
Enter guess: You incorrectly guessed 67
Enter guess: You incorrectly guessed 23
Enter guess: You incorrectly guessed 11
Enter guess: You win! The number was 14



It's important to note that the input file's entries are not producing new line on each read. I don't currently know how to do that. Regardless, let's say you looked the output over, and it's functioning properly, so you deem this output to be a correct run. You can now save this to a text file for later comparisons by redirecting the output to a file.

guess.exe < guesses.txt > test1.txt



You will notice no output is produced, but the exact same output is now in the test1.txt file that was just created.

From this point forward, we can run it to produce a new output file, which we will compare against our verified test1.txt file.

guess.exe < guesses.txt > guesses_run.txt



Get yourself a diff tool for your system, and then directly compare two files to see the differences. If no differences exist, the two outputs are the same, and since you verified test1.txt to be correct, you know that what is produced is still correct.

Streamline this to have a collection of input files with corresponding output files, and write a script that runs your program with each input file to produce a current_run output file, and then diff compare every produced file to its corresponding expected output file to see that all tests are passing at the I/O level.

We won't get into the implementation of this, but hopefully this guide has given you some ideas about how you can begin to introduce this technique into your own projects to increase your productivity.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1