HPlogo Accessing Files Programmer's Guide: HP 3000 MPE/iX Computer Systems > Chapter 9 Reading from a File

Reading From $STDIN

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

 » Index

You can read data from your program's standard input device ($STDIN) by using one of the following intrinsics:

  • READ

  • READX

  • FREAD

The job/session input device is the source of all MPE/iX commands relating to a job or session and is the primary source of all ASCII information input to the job or session. You can read a string of ASCII characters from the job/session input device into an array in your program with the READ and READX intrinsics. The READ and READX intrinsics are identical, except that the READX intrinsic reads input from $STDINX instead of $STDIN. The $STDINX file is equivalent to $STDIN, except that records with a colon (:) in the first column of a line indicate the end-of-file to $STDIN, and only the commands :EOD, and EOF indicate the end of file for $STDINX.

NOTE: The READ and READX intrinsics are limited in their usefulness in that FILE commands are not allowed. In addition, you cannot use the FCHECK intrinsic to determine error conditions encountered by READ or READX. You may find it more convenient (and a better programming practice) to use the HPFOPEN/FOPEN intrinsic to open the files $STDIN or $STDINX, then issue FREAD calls against these files.

If the standard input device ($STDIN) and the standard list device ($STDLIST) are opened with an HPFOPEN/FOPEN intrinsic call, the FREAD and FWRITE intrinsics can be used with these devices. For example, the FREAD intrinsic can be used to transfer information entered from a terminal to a buffer in the stack, and the FREAD intrinsic can be used to transfer information from your stack directly to the standard list device.

Example 9-3 is an HP Pascal/iX code segment that uses the PRINT intrinsic to prompt a user for a file designator, then uses the READ intrinsic to read the input from $STDIN. Assume that the file designator is then returned to a procedure that calls HPFOPEN to open a file with the formaldesignator option passing the file name specified by the user.

Example 9-3. Reading from $STDIN Using READ

   procedure get_file_designator (var file_name : packed array [1..80] of

   char);



   var

      message         : packed array [..80] of char;  {holds prompt to user }

      length          : shortint;                     {length of prompt     }

      control_code    : shortint;                     {required by PRINT    }

      read_length     : shortint;                     {length read by READ  }

      expected_length : shortint;                     {size of message array}



   begin

      massage :='Please input a valid file reference'; {specify prompt      }

      length := -35;                                   {length of prompt    }

      control_code := 0                                {default condition   }

      expected_length := -80                           

      PRINT (message,

             length,

             control_code

            );

      if ccode <> cce then handle_file_error;

      else begin

            read_length := READ ( file_name,          {read data to output parm}

                                  expected_length     {length of file_name     }

                        );

            if ccode <> cce then handle_file_error;

        end

   end;

If an error is encountered by either READ or PRINT, procedure handle_file_error is invoked. For more information about READ parameters, refer to the MPE/iX Intrinsics Reference Manual (32650-90028). For more information about using the PRINT intrinsic, refer to chapter 8, "Writing to a File". For more information about opening a file, refer to chapter 5, "Opening a File". For more information about file designators, refer to chapter 3, "Specifying a File Designator". In appendix A, "HP Pascal/iX Program Examples", example A-2 uses a routine similar to example 9-3 to prompt the user for a valid file reference.

Feedback to webmaster