FAQs about Comparing FIles and I/O Redirection


How can I compare my output to the sample output?

First, get a copy of your output using the same inputs as we used to create the sample output. You can save the output in a variety of ways. See the Programming Environment pages about MSVC and Codewarrior. But a good technique is I/O redirection, which can be used to automate the process.

Second, see if your output file matches our output file.

How do I tell if my output file matches the sample output file?

1. Use an old data-cruncher's trick: Print out the two files with identical fonts, margins, etc. Lay the two pieces of paper on top of each other, hold them up towards a strong light, and look through them. Line them up, and any differences will usually be obvious.

2. Unix has a diff command that compares two files. With a little experience, you can use it to pinpoint where two files are different. For example, type in

diff my_output.txt sample_output.txt

and diff will tell you whether the files are identical, or print out a list of the different lines along with some gobbledegook.

3. DOS has an "fc" command that is like Unix's diff.

4. CodeWarrior has a "Compare files" command (under "Search") - choose the two files, and it gives you a cool split-window display highlighting where the files are different.

5. MSVC comes with a utility named "Windiff" that is something like CodeWarrior's Compare Files command, but Windiff is a separate application that you will have to find and execute.

6. Some text editors have a comparison feature - check your favorite. Tell us yours, and we'll add it here.


What is I/O redirection?

Input/output (I/O) redirection means connecting input and output streams to a different device than they are normally connected to. In particular, you can redirect the standard input stream (cin or stdin) from the keyboard to an input file, and the standard output stream (cout or stdout) from the display to an output file. Thus you can have your program taking input from a file instead of the keyboard, and sending it to a file instead of the screen, without having to change the I/O statements in your program. Rather, cin and cout are connected to files instead of the keyboard and screen.

Why would I redirect I/O?

The reason for redirecting I/O in this course is simply as a way to automate the process of demonstrating or testing your program. For example, the autograder uses redirection to supply test inputs to your program and record its outputs.

For your own testing, instead of your having to carefully type in the test input exactly right, you can have your program read the test input from a file instead. Instead of fiddling around in Wintel or Unix with trying to copy-paste the output window into a text document, you can have your program simply write its output directly to a file.

Using I/O redirection is not required; it is simply a potential convenience.


What are the disadvantages of redirecting I/O?

One drawback is that the keyboard input is not written to the output file, so your program has to "echo" the input in some way in order for there to be a record of it in the output file. In other words, the output file will not include the input from the keyboard (or the input file). This is OK, once you get used to it.

Another drawback is if output is redirected, you don't get to see it on the screen, making it hard to tell what your program is doing. Thus you only want to use I/O redirection for a final "production run" or a testing run - you can't usefully interact with the program if I/O is redirected.

Finally, once I/O is redirected to a file, the keyboard and/or the display will no longer be connected to the program. You will have to let the program finish running and start it again before you can do normal keyboard/screen I/O with it again.

How do I redirect I/O?

You can do this in *any* standard C/C++ programming environment using our handy "redirect_io" function - this prompts for filenames and uses an obscure function, freopen, in the stdio library, to do the redirection from your program. It's pretty simple and should work for any C/C++ implementation.

To get the code for redirect_io() to copy-paste into your source code, download the file redirect_io_s.cpp. Follow the instructions in the comments.

To get the header file and implementation file for a multiple-file project, download the files redirect_io.h and redirect_io.cpp. Follow the instructions in the comments.

You can also download these files with ftp from the course directory at

/afs/engin.umich.edu/class/perm/eecs280/Public/redirect

The MacOS Codewarrior IDE includes a console.h facility which provides an emulated command line interface for your program (see the big Using CodeWarrior handout), which allows you to do a Unix-like redirection and command line parameters, but it is harder to use than our redirect_io function.

How can I redirect I/O without changing my code?

If you don't want to use our redirect_io function, then you can do the following:

For Windows IDEs, run the program under DOS. Say your executable in p.exe, and the input is in a file, e.g. inp.txt. Then, from the DOS command line, type:

C:\prompt> p.exe < inp.txt > out.txt

Your output will be left in out.txt.

Unix uses the same command line syntax: '<' means take the input from the following file, and '>' means put the output in the following file.

For CodeWarrior, it is as easy as pie to save a copy of your input and output just by saving the console window with the standard file/save command or responding to the prompt when you quit the program. This saves both the input and output in the file, but you have to type the input in by hand. You can do a similar save of the console window in MSVC, but some Windows-fiddling is required (see the pages about MSVC).