home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / mystic.zip / PAS6.DOC < prev    next >
Text File  |  1986-02-27  |  16KB  |  914 lines

  1.  
  2.         Mystic Pascal  User Manual                                      59
  3.  
  4.  
  5.         9.  Input/Output
  6.  
  7.              Mystic  Pascal  supports all standard Pascal I/O  procedures 
  8.         and  functions  and has extensions to support  random  disk  file 
  9.         access and to assign a DOS file name to a Pascal file variable.
  10.  
  11.         +    ASSIGN         associate a file name with a file variable
  12.         +    CLOSE          terminate processing on a file 
  13.              EOF            Boolean function indicates end-of-file 
  14.                             condition
  15.              EOLN           Boolean function indicates end-of-line
  16.                             condition for text files
  17.         1.6  GET            move file pointer to next component
  18.         +    IORESULT       integer function status of I/O operation
  19.              PAGE           advance textfile output to new page
  20.         1.6  PUT            append buffer variable to file
  21.              READ           obtain input from console or disk file
  22.              READLN         obtain textfile input from new line
  23.              RESET          prepare an existing file for input
  24.              REWRITE        create a file for output
  25.         +    SEEK           position the file pointer for random access
  26.              WRITE          output data to console, printer, disk file
  27.              WRITELN        output textfile data and terminate line
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.         Section 9:   Input/Output
  58.  
  59.         Mystic Pascal  User Manual                                      60
  60.  
  61.  
  62.         9.1  File Variables
  63.  
  64.              File variables,  like other variables, are declared in a VAR 
  65.         section.  They indicated the data type of the file's components.
  66.  
  67.              TYPE
  68.              DATAFILE = FILE OF REAL;
  69.              VAR
  70.              F1 : DATAFILE;
  71.              F2 : FILE OF ARRAY [0..63] OF CHAR;
  72.              F3 : TEXT;
  73.  
  74.              Textfiles   are  declared  by  the  predefined  type   TEXT.  
  75.         Textfiles consist of lines of characters separated by the end-of-
  76.         line byte sequence 0DH, 0AH (carriage return, line feed).
  77.  
  78.  
  79.  
  80.         9.2  Standard Files
  81.  
  82.              The  DOS operating system supports several standard  "files" 
  83.         or logical devices which are always available.  These may be used 
  84.         directly  as  file variables in Read/ln and Write/ln.   They  may 
  85.         also  be  assigned  to  Pascal  file  variables  by  the   Assign 
  86.         procedure.
  87.  
  88.              CON       standard console device  (input/output)
  89.              KBD       keyboard device  (input without echoing)
  90.              AUX       auxiliary device  (input/output)
  91.              LST       printer  (output)
  92.              PRN       printer  (output)
  93.  
  94.              If  no file variable is specified as the first parameter  in 
  95.         the  Read/ln or Write/ln procedures,  the default files INPUT and 
  96.         OUTPUT respectively are used.  These are assigned to the standard 
  97.         console and keyboard devices.
  98.  
  99.  
  100.  
  101.         9.3  How to Send Data to Your Printer
  102.  
  103.              To  route  output to your printer specify the Standard  File 
  104.         LST or PRN in your WRITE or WRITELN statements. 
  105.  
  106.              WRITELN( PRN, 'This line will be printed.');
  107.  
  108.              WRITE( PRN, 'X =',X,'   Y =',Y,'   Z =',Z );
  109.  
  110.  
  111.  
  112.  
  113.  
  114.         Section 9:   Input/Output
  115.  
  116.         Mystic Pascal  User Manual                                      61
  117.  
  118.  
  119.         9.4  ASSIGN        (Non-Standard Feature)
  120.  
  121.              ASSIGN( Filvar, String )
  122.  
  123.              ASSIGN( Filvar, Standard_file )
  124.  
  125.              The  procedure  Assign is used to associate a file  variable 
  126.         with a particular disk file or standard file.
  127.  
  128.              The  ability  to  assign standard files to a  file  variable 
  129.         allows  complete I/O redirection within a  Pascal  program.   For 
  130.         example,  a  program which creates reports could route its output 
  131.         to a disk file one time and to a printer or console another time.  
  132.         There  is no need for separate WRITE statements for each type  of 
  133.         output.
  134.  
  135.              10: WRITELN('Select output device for Report Listing');
  136.                  WRITELN('1 = Console   2 = Printer   3 = Diskfile');
  137.              READLN(X);
  138.              CASE X OF
  139.              1 : ASSIGN( REPORTFILE, CON );
  140.              2 : ASSIGN( REPORTFILE, PRN );
  141.              3 : ASSIGN( REPORTFILE, 'REPORT.LST' );
  142.              ELSE GOTO 10
  143.              END; 
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.         Section 9:   Input/Output
  172.  
  173.         Mystic Pascal  User Manual                                      62
  174.  
  175.  
  176.         9.5  CLOSE         (Non-Standard Feature)
  177.  
  178.         CLOSE( Filvar )
  179.  
  180.              The   Close  procedure  must  be  called  after   completing 
  181.         processing  on  a disk file to ensure that the disk directory  is 
  182.         updated.  Failure to Close a file after updating it may result in 
  183.         the loss of data.
  184.  
  185.              Closing  a file variable to which a standard file  has  been 
  186.         assigned is treated as a null operation.
  187.  
  188.  
  189.              CLOSE( F1 );
  190.  
  191.              CLOSE( REPORTFILE );
  192.  
  193.              CLOSE( SORTFILE2 );
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.         Section 9:   Input/Output
  229.  
  230.         Mystic Pascal  User Manual                                      63
  231.  
  232.  
  233.         9.6  EOF
  234.  
  235.         EOF( Filvar )
  236.  
  237.              Eof  is  a Boolean function which indicates the  end-of-file 
  238.         condition.   Eof  is true only when the file pointer points  past 
  239.         the last component of the file, otherwise it is false.
  240.  
  241.  
  242.              { Copy F1 into F2 }
  243.              WHILE NOT EOF( F1 ) DO
  244.                   BEGIN
  245.                   READ( F1, X );
  246.                   WRITE( F2, X )
  247.                   END
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.         Section 9:   Input/Output
  286.  
  287.         Mystic Pascal  User Manual                                      64
  288.  
  289.  
  290.         9.7  EOLN
  291.  
  292.         EOLN
  293.  
  294.         EOLN( Filvar )
  295.  
  296.              Eoln  is a Boolean function which indicates the  end-of-line 
  297.         condition of a textfile.   If no file variable is specified,  the 
  298.         function assumes the file INPUT.   It is a compiler error if  the 
  299.         file variable is not a textfile.  
  300.  
  301.  
  302.              { Average N numbers from the console }
  303.              SUM := 0;      N := 0;
  304.              WHILE NOT EOLN DO
  305.                   BEGIN
  306.                   READ( X );
  307.                   SUM := SUM + X;
  308.                   N := N + 1
  309.                   END
  310.              WRITELN( 'The average is', SUM DIV N );
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.         Section 9:   Input/Output
  343.  
  344.         Mystic Pascal  User Manual                                      65
  345.  
  346.  
  347.         9.8  GET, PUT and Buffer Variables                  ** 1.6 **
  348.  
  349.              Standard  Pascal supports Input/Output operations which  are 
  350.         more  primitive than Read and Write.   In fact Read and Write are 
  351.         defined in terms of the procedures Get and Put,  which only  move 
  352.         data between a file and its buffer variable.
  353.  
  354.              A  buffer variable is associated with each file.   Its  data 
  355.         type  is  the  same  as the components of  the  file.   A  buffer 
  356.         variable  is accessed by using the file variable's name  followed 
  357.         by an uparrow.
  358.  
  359.              WRITE( F, X )  is equivalent to    F^ := X;  PUT( F )
  360.  
  361.              READ( F, X )   is equivalent to    GET( F );  X := F^
  362.  
  363.  
  364.  
  365.         GET( Filvar )
  366.  
  367.              The  Get  procedure advances the file pointer  to  the  next 
  368.         component of the file.  The current component is available in the 
  369.         file's buffer variable.
  370.  
  371.  
  372.  
  373.         PUT( Filvar )
  374.  
  375.              The  Put procedure appends the value in the buffer  variable 
  376.         to  the file.   The condition EOF( Filvar ) must be true when Put 
  377.         is called.
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.         Section 9:   Input/Output
  400.  
  401.         Mystic Pascal  User Manual                                      66
  402.  
  403.  
  404.         9.9  IORESULT
  405.  
  406.         IORESULT
  407.  
  408.              This  integer  function gives the return code from the  last 
  409.         Input/Output  operation.   It should be checked after each  RESET 
  410.         and REWRITE to be sure that the operation completed successfully.  
  411.         The IORESULT values are the same values returned by DOS.
  412.  
  413.              0         Successful completion
  414.              1         Invalid function number
  415.              2         File not found
  416.              3         Path not found
  417.              4         Too many open files (no handles left)
  418.              5         Access denied
  419.              6         Invalid handle
  420.              7         Memory control blocks destroyed
  421.              8         Insufficient memory
  422.              9         Invalid memory block address
  423.              10        Invalid environment
  424.              11        Invalid format
  425.              12        Invalid access code
  426.              13        Invalid data
  427.              14        (not used)
  428.              15        Invalid drive was specified
  429.              16        Attempted to remove the current directory
  430.              17        Not same device
  431.              18        No more files
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.         Section 9:   Input/Output
  457.  
  458.         Mystic Pascal  User Manual                                      67
  459.  
  460.  
  461.         9.10  PAGE
  462.  
  463.         PAGE
  464.  
  465.         PAGE( Filvar )
  466.  
  467.              The  Page procedure causes the textfile to begin printing on 
  468.         a new page if printed on a suitable device.   If no file variable 
  469.         is specified, the file OUTPUT is assumed.  It is a compiler error 
  470.         if the file variable is not a textfile.   If the file contains  a 
  471.         partial line of text, an implicit Writeln is issued.
  472.              
  473.              Page always outputs a form feed character CHR(12).
  474.  
  475.  
  476.              PAGE;
  477.  
  478.              PAGE( REPORTFILE );
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.         Section 9:   Input/Output
  514.  
  515.         Mystic Pascal  User Manual                                      68
  516.  
  517.  
  518.         9.11  READ
  519.  
  520.         READ( variable_list )
  521.  
  522.         READ( Filvar, variable_list )
  523.  
  524.              The Read procedure is used to obtain input from a disk  file 
  525.         or  standard  file  such  as the  console  keyboard.   The  input 
  526.         value is stored directly into one or more variables.   If no file 
  527.         variable is specified, the INPUT file is assumed.
  528.  
  529.  
  530.  
  531.              READ( X, Y, Z );
  532.  
  533.              READ( F1, CUSTOMER );
  534.  
  535.              FOR I := 1 TO 100 DO  READ( F2, X[I] );
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.         Section 9:   Input/Output
  571.  
  572.         Mystic Pascal  User Manual                                      69
  573.  
  574.  
  575.         9.12  READLN
  576.  
  577.         READLN
  578.  
  579.         READLN( variable_list )
  580.  
  581.         READLN( Filvar )
  582.  
  583.         READLN( Filvar, variable_list )
  584.  
  585.              The  Readln procedure is similar to Read but may be  applied 
  586.         only to textfiles.  After the Readln has been processed, the file 
  587.         pointer  is  set  to the beginning of the  following  text  line.  
  588.         Unused data on the current line is skipped over.
  589.  
  590.  
  591.  
  592.              READLN;        {Skip to next line of keyboard input}
  593.  
  594.              READLN( DATAFILE, PARM1, PARM2, PARM3 );  {ignore parm4,5}
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.         Section 9:   Input/Output
  628.  
  629.         Mystic Pascal  User Manual                                      70
  630.  
  631.  
  632.         9.13  RESET
  633.  
  634.         RESET( Filvar )
  635.  
  636.              The  Reset  procedure is used to open an existing disk  file 
  637.         for  input or output operations.   An Assign procedure must  have 
  638.         been  used  before Reset to assign a disk file name  or  standard 
  639.         file with the file variable.  Reseting a file variable to which a 
  640.         standard file has been assigned is treated as a null operation.
  641.  
  642.              If  a  disk file is not found by  Reset,  a  run-time  error 
  643.         occurs.
  644.  
  645.              
  646.              RESET( F2 );
  647.  
  648.              RESET( DATAFILE );
  649.  
  650.              RESET( TEXTFILE );
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.         Section 9:   Input/Output
  685.  
  686.         Mystic Pascal  User Manual                                      71
  687.  
  688.  
  689.         9.14  REWRITE
  690.  
  691.         REWRITE( Filvar )
  692.  
  693.              The  Rewrite procedure is used to create a new disk file for 
  694.         output and input.  If a disk file with the same file name already 
  695.         exists, it is deleted before the new file is created.
  696.  
  697.              An  Assign procedure must have been used before  Rewrite  to 
  698.         assign  a disk file name or standard file to the  file  variable.  
  699.         Rewriting  a  file  variable to which a standard  file  has  been 
  700.         assigned is treated as a null operation.
  701.  
  702.              If the disk directory is full, a run-time error occurs.
  703.  
  704.  
  705.              REWRITE( F4 );
  706.  
  707.              REWRITE( REPORTFILE );
  708.  
  709.              REWRITE( DATAFILE );
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.         Section 9:   Input/Output
  742.  
  743.         Mystic Pascal  User Manual                                      72
  744.  
  745.  
  746.         9.15  SEEK         (Non-Standard Feature)
  747.  
  748.         SEEK( Filvar, N )
  749.  
  750.              The  Seek  procedure positions the file pointer at  the  Nth 
  751.         component of the file.  The first component is number 0.  It is a 
  752.         compiler error if the file variable is a textfile.
  753.  
  754.              Seek is used for random access file processing.
  755.  
  756.  
  757.              SEEK( F1, INDEX );
  758.  
  759.              SEEK( DOSSIER, AGENTNUM );
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.         Section 9:   Input/Output
  799.  
  800.         Mystic Pascal  User Manual                                      73
  801.  
  802.  
  803.         9.16  WRITE
  804.  
  805.         WRITE( variable_list )
  806.  
  807.         WRITE( Filvar, variable_list )
  808.  
  809.              The  Write  procedure  is used to output data  values  to  a 
  810.         textfile or non-textfile.   If no file variable is specified, the 
  811.         OUTPUT file is assumed.  If the file is a textfile then integers, 
  812.         reals and Booleans are automatically converted to text format.
  813.  
  814.  
  815.  
  816.              WRITE('Enter longitude, latitude of target : ');
  817.  
  818.              WRITE( REPORTFILE, CHR(9), 'SALES SUMMARY FOR ',MONTH );
  819.  
  820.              WRITE( MODEM, SIGNON );
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.         Section 9:   Input/Output
  856.  
  857.         Mystic Pascal  User Manual                                      74
  858.  
  859.  
  860.         9.17  WRITELN
  861.  
  862.         WRITELN
  863.  
  864.         WRITELN( variable_list )
  865.  
  866.         WRITELN( Filvar )
  867.  
  868.         WRITELN( Filvar, variable_list )
  869.  
  870.  
  871.              The  Writeln  procedure is similar to Write but may only  be 
  872.         applied to textfiles.   After the variables on the list have been 
  873.         output,  a standard end-of-line byte sequence is output.  This is 
  874.         (carriage_return, line_feed) 13, 10.
  875.  
  876.  
  877.  
  878.              PROCEDURE SKIP ( X : INTEGER );
  879.              VAR I : INTEGER;
  880.              BEGIN
  881.              IF (X > 0) AND (X <= 60) THEN
  882.                   FOR I := 1 TO X DO WRITELN 
  883.              END;
  884.  
  885.              
  886.              WRITELN( F1 );
  887.  
  888.              WRITELN( F1, X, Y, Z );
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.         Section 9:   Input/Output
  913.  
  914.