* * * REDIRECTION * * * REDIRECTION * * * REDIRECTION * * *

* * * CAPTURING RUNTIME OUTPUT * * * CAPTURING RUNTIME OUTPUT * * *

INTRODUCTION
============

It seems that some students are having difficulty with 
getting REDIRECTION to work properly...

Possibly the problem is that you've yet to completely grasp 
the concepts involved in "redirection"...  Or, possibly you
never learned about redirection in class -- or you missed
that lecture...  So, here's yet another description of
what's going on...

(Remember, there's a description of Redirection for the PC 
in the CIS 110A Manual, Sect. 1.08, and Redirection for the 
Mac in the Manual, Sect. 1.09.  But, perhaps you don't have 
your copy of the Manual handy...  Or, perhaps you're in
CIS 110B or CIS 110K or some other class... )

WHY DO WE CARE?
===============

There are two critical reasons why you care about redirection:

1) You want to be able to create a program and then run it
   again and again, using different input, WITHOUT having to
   physically type and retype and retype all the input...
   You want to be able to place your input data into file(s)
   and have your program READ its input from a file.  And,
   you don't want to bother creating true Java/C++ file objects.

2) You want to be able to capture ALL of your output and
   place it into a file.  Output from most of your programs
   will NOT fit on one screen -- it will extend far beyond
   the limits of your screen.  And, even if you have 
   screen-buffering engaged, sometimes your output will
   exceed the capacity of the screen buffer.  You MUST be
   able to capture and later print ALL the output from your
   program.  So, something so simple as "PrintScreen" just
   won't do the trick.  So, you want the ability to force
   the output from your program to be placed into a file --
   on your hard disk.  Then you will capture ALL of the 
   output; you can examine it before committing it to paper;
   you can save the output from several runs of your program
   in different files -- and compare them online, or print
   them all at once; etc.

REDIRECTING INPUT
=================

Think about what happens when you run a program and it has 
a statement in it like:

     System.in.read(xNumber);     // Java
     cin >> nBoys;                // C++

Each time the program encounters this statement during 
execution, it is saying:  Go to standard input, grab the 
next integer that is entered there, and store it away in 
nBoys...  Oh, and by the way, ignore leading whitespace 
while you're doing this; and, you (the computer) know what 
I mean by an "integer"...

OK.  You run the program...  It comes to the point at which 
the cin wants to read an integer...  You type an integer at 
the keyboard...  The program grabs it and stores it away...  
"vanilla" no problem!

Now, if you REDIRECT input when you run the program, so 
that it now takes its input from a file rather from a 
keyboard...  THERE'S NOTHING DIFFERENT!!

In particular, when the program gets to the cin statement, 
it goes to standard input -- now redirected from the 
keyboard to your data file; it reads the NEXT integer that 
it sees -- just like it grabbed the NEXT integer that you 
typed -- and tucks it away in nBoys...  And, the next time 
that the program comes across the cin statement...  it gets 
the NEXT integer that it sees -- the one AFTER the last one 
that it read, just like it gets the NEXT one that you 
typed...

WHAT DOES INPUT DATA LOOK LIKE?
===============================

How do you separate data when you're typing at the keyboard?  
With whitespace.  Therefore, what should you use to separate 
data in your data file?  With whitespace.  Should you use 
commas to separate numbers?  No!  Unless your program is 
specifically set up to understand that commas are separators,
you don't use them when you're entering data from the 
keyboard, nor when stdin is redirected to a file.

REDIRECTING OUTPUT
==================

OK, so much for input...  What about output?  What happens
when you say:

    System.out.println("Hello CCSF students!");  // Java
    cout << "Hello CCSF students!" << endl;      // C++

Sending data to 'out/cout' causes the output to placed onto
stdout -- which, if it's not redirected, is your display.
If you redirect stdout to a file (or to a printer, or to a
modem line, or to a ...) Java/C++ neither knows nor cares...
It does exactly the same thing:  it puts the data onto stdout.
But, since stdout has been redirected, the output appears
not on your display, but rather on/in whatever file or
device to which you've redirected it...

Java/C++ HAVEN'T A CLUE ABOUT "REDIRECTION"...
=============================================

The CRITICAL THING TO NOTE IS THIS:  "redirection" has
NOTHING WHATEVER to do with Java/C++.  Redirection is 
handled at the operating system level.  Java/C++ has no 
knowledge about redirection.  Redirection is NOT a Java/C++ 
concept.  The identical Java/C++ program can be used both 
with and without redirection.

CREATING A PROGRAM TO USE WITH REDIRECTION
==========================================

Now, HOW do you CREATE a program that has the POSSIBILITY of
redirection?  That depends ENTIRELY on what operating system 
and which compiler you happen to be using.  The general rule 
is:  You MUST create an "executable file" in order to use 
redirection.  Say that your executable is "Tutor.exe" (or 
a.out, or whatever).  Then, to redirect your program's INPUT,
you issue the COMMAND at the COMMAND LINE PROMPT:

     C> java Tutor < myfile  [Enter]   (on your PC - Java)
     C> Tutor < myfile  [Enter]        (on your PC - C++)

or

     $ java Tutor < myfile  [Enter]    (on UNIX - Java)
     $ a.out < myfile  [Enter]         (on UNIX - C++)

(This assumes that you want your program to take its input 
from the file named "myfile" that lives in the same directory 
as does "Tutor.class" or "Tutor.exe" or "a.out."

How do you redirect your program's OUTPUT?  You issue the
COMMAND and the COMMAND LINE PROMPT:

     C> java Tutor > ToPrint.txt  [Enter]  (on your PC - Java)
     C> Tutor > ToPrint.txt  [Enter]       (on your PC - C++)

or

     $ java Tutor > ToPrint.txt  [Enter]   (on UNIX - Java)
     $ a.out > ToPrint.txt  [Enter]        (on UNIX - C++)

NAMING OUTPUT FILES
===================

Personally, I ALWAYS name files that contain "flat ASCII
human-readable text" with a .txt extension (even in UNIX).
That way I ALWAYS know the kind of file it is -- just from
how it's named...    And, in Windows of course, if you
double-click a .txt file, NotePad launches and displays
the file...   and you can PRINT the (entire) file using
NotePad.

CREATING AN EXECUTABLE FILE IN JAVA (FOR REDIRECTION)
=====================================================

You're in luck!.  Since the Java runtime interpreter
(the JVM - Java Virtual Machine) already is executable, 
EVERY Java application automatically also is ready to be 
used with redirection.  You have NOTHING extra to do!

REDIRECTION AND RealJ
=====================

If you're running RealJ...   You DO NOT NEED I/O redirection
to capture your output!   RealJ sends the console output to 
the "message window"  That means that all you have to do is
to run your ap, then select all of the message window, and 
copy it, then paste it into whatever you want...  like
NotePad...  and print or do whatever you want with it...

However, if you REALLY DO want to use conventional 
redirection to capture output, and if you want to use
conventional redirection to redirect input...
YOU CAN DO THIS, TOO...    Just open a DOS window, go to
the directory in which your .java and .class files reside...
Then,   run your ap normally...

java  HelloWorld  < input.txt  > output.txt  [Enter]

this will have HelloWorld (or whatever the Java ap is...)
take its stdin input from the input.txt file...  and will
redirect its output to the output.txt  file...  output.txt
will be (re-)created each time you run the program...    
Since both input.txt and output.txt are normal, "vanilla" 
text files, you can use ANY text editor to create them, 
view them, print them, etc...    like NotePad...   -grin-

CREATING AN EXECUTABLE FILE IN C++ (FOR REDIRECTION)
====================================================

HOW do you create an "executable file"?  You've probably been 
doing this for every assignment.  If you're running Borland's 
Turbo C++ V3.0, then each time you BUILD, MAKE, or RUN you 
create an executable file (one that has an .exe 
suffix/extension).  If you're running in UNIX, then each time 
you run CC you create one (called a.out).  If you're using 
Microsoft Visual C++, then you need to create a "Console 
Application" when you select that kind of Project you wish to 
build.

If you're using Borland's V4.x or V5.x IDE, then you need to 
create an "Application [.EXE]" when you select the kind of 
Project you wish to build.  For other compilers, IDEs and 
environments -- you'll need to read the appropriate system 
documentation.  (I may be able to guide you a little bit -- 
ask!)  Note:  If your compiler/IDE/environment DOESN'T allow 
you to create a COMMAND-LINE driven application (i.e., a "DOS
application," and ONLY allows you to create a "Windows 
Application," THEN you are WELL AND TRULY UP THE CREEK:  
Because Windows does NOT provide a mechanism for redirection!

A NOTE FOR MACINTOSH USERS
==========================

For Mac users, redirection is handled ENTIRELY differently...
Depending upon which compiler and IDE you're using, there 
will be some kind of dialog that appears (or can be made to 
appear) that allows you to specify where you want your stdin 
input and stdout output to come from/go to.  Mac users should 
take a look at the sample program called Redirect.cpp -- 
available via the "Additional examples" link off the class 
Home Page.

Is this any more clear now?   Hope so...

Last updated:  Jan. 10, 2001 - 22:00 PST