Inputting from a File

The Input symbol is used to input data from a text file.  When an Input symbol is reached during Raptor program execution, the system determines whether or not input has been redirected.  If input has been redirected, meaning an input file has been specified, the input is taken from the specified file.  If input has not been redirected, it is performed interactively.  That is, the user is prompted, and the input is taken from the keyboard.

 

Redirecting Input from a File

 

Raptor provides two versions of the procedure Redirect_Input to redirect input from 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_Input as shown in the examples below:

 

Redirect_Input("file.txt")

Redirect_Input("C:\Documents and Settings\John.Doe\CS110\file.txt");

 

In the first example, only the file name is given.  In this case, the specified text file must reside in the same directory as the current Raptor program.  In the second example, the full path to the file is given.  In both cases, the file extension (".txt" in the above examples) is required, as any extension is acceptable.  The specified file will be used for subsequent input operations.  

 

Note:  If the file name is spelled incorrectly, or if it doesn't exist in the specified location,  a run-time error will occur.

 

The second version of Redirect_Input redirects input with a simple yes or true argument:

 

Redirect_Input(True)

 

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

 

File Input

 

While simple at first glance, the details of file input are tricky enough that the details are on a separate page.

 

Stopping Input Redirection

 

After a successful call to Redirect_Input, the program gets its input from the specified file.  To reset Raptor so that subsequent Input symbols get their input from the user, another call to Redirect_Input is used, this time with a False (No) argument:

 

Redirect_Input(False)

 

After this call is executed, the input file is closed, and subsequent inputs will again come from the user at the keyboard.  Note that, if the same file is opened again later during program execution, input will again start from the beginning of the file.  Raptor doesn't remember where it left off the last time a file was used for input.

 

Example

 

The chart below shows how the Redirect_Input call is used to switch from interactive to file input.  The first input box gets the number of items to process from the user.  After that, the Redirect_Input call tells Raptor that subsequent inputs will come from the file "file.txt".  Once the array elements have been read from the file, input redirection is turned off.  The last input again prompts the user and takes input from the keyboard.