Outputting to a File

The Output symbol is used to output data to a text file.  When an Output symbol is reached during Raptor program execution, the system determines whether or not output has been redirected.  If output has been redirected, meaning an output file has been specified, the output is written to the specified file.  If output has not been redirected, it goes to the Master Console.

 

Redirecting Output to a File

 

Raptor provides two versions of the procedure Redirect_Output to redirect output to a file.  Both of them will appear in a call symbol.  In the first, the programmer specifies a file name as an argument to Redirect_Output as shown in the examples below:

 

Redirect_Output("file.txt")

Redirect_Output("C:\Documents and Settings\John.Doe\CS110\datafile");

 

In the first example, only the file name is given.  In this case, the specified text file will be created in the same directory as the current Raptor program.  In the second example, the full path to the file is given.  Note also in the second example that no file extension is given.  In this case, the file datafile will be created with no extension.  The specified file will be used for subsequent output operations.  

 

Notes:  If the file specified already exists, it will be overwritten with no warning!  All of the file's previous contents will be lost!  If the file specified cannot be written to (already exists as  read-only file, error in path, etc.), a run-time error will occur.

 

The second version of Redirect_Output redirects output with a simple yes or true argument:

 

Redirect_Output(True)

 

This delays the selection of the output file to run time.  When the Call symbol containing Redirect_Output is executed, a file selection dialog box will open, and the user can specify which file is to be used for output.

 

File Output

 

The output will appear in the file in the same format as the Master Console.  In the Output symbols, the programmer can control what is output and when new lines are started.

 

Stopping Output Redirection

 

After a successful call to Redirect_Output, the program writes its output to the specified file.  To reset Raptor so that subsequent Output symbols write their output to the Master Console, another call to Redirect_Output is used, this time with a False (No) argument:

 

Redirect_Output(False)

 

After this call is executed, the output file is closed, and subsequent outputs will again go to the Master Console.  

 

Example

 

The chart below shows how the Redirect_Output call is used to switch from interactive to file output.  The first output box displays a message to the Master Console.  After that, the Redirect_Output call tells Raptor that subsequent outputs will go to the file "file.txt".  Once the array elements have been written to the file, output redirection is turned off.  The last output again goes to the Master Console.