9.7 File Operations

  • Read( name-file ) O

    reads the input from the file with the filename name-file, this filename must be given as a string.

    Read first opens the file name-file. If the file does not exist, or if GAP cannot open it, e.g., because of access restrictions, an error is signalled.

    Then the contents of the file are read and evaluated, but the results are not printed. The reading and evaluations happens exactly as described for the main loop (see Main Loop).

    If a statement in the file causes an error a break loop is entered (see Break Loops). The input for this break loop is not taken from the file, but from the input connected to the stderr output of GAP. If stderr is not connected to a terminal, no break loop is entered. If this break loop is left with quit (or ctr-'D') GAP exits from the Read command, and from all enclosing Read commands, so that control is normally returned to an interactive prompt. The QUIT statement (see QUIT) can also be used in the break loop to exit GAP immediately.

    Note that a statement must not begin in one file and end in another, i.e., eof (end-of-file) is not treated as whitespace, but as a special symbol that must not appear inside any statement.

    Note that one file may very well contain a read statement causing another file to be read, before input is again taken from the first file. There is an operating system maximum dependent on the number of files that may be open simultaneously. Usually it is 15.

  • ReadAsFunction( name-file ) O

    reads the file with filename name-file as a function and returns this function.

    Example

    Assume that the file ``/tmp/example.g'' contains the following

    local a;
    
    a := 10;
    return a*10;
    

    Reading the file as a function will not affect a global variable a.

    gap> a := 1;
    1
    gap> ReadAsFunction("/tmp/example.g")();
    100
    gap> a;
    1
    

  • PrintTo( name-file ) F

    works like Print, except that the output is printed to the file with the name name-file instead of the standard output. This file must of course be writable by GAP. Otherwise an error is signalled. Note that PrintTo will overwrite the previous contents of this file if it already existed. AppendTo can be used to append to a file (see AppendTo).

    There is an operating system dependent maximum on the number of output files that may be open simultaneously, usually this is 14.

  • AppendTo( name-file ) F

    works like PrintTo, except that the output does not overwrite the previous contents of the file, but is appended to the file.

  • LogTo( name-file ) O

    causes the subsequent interaction to be logged to the file with the name name-file, i.e., everything you see on your terminal will also appear in this file. This file must of course be writable by GAP, otherwise an error is signalled. Note that LogTo will overwrite the previous contents of this file if it already existed.


    LogTo()

    In this form LogTo stops logging.

  • InputLogTo( name-file ) O

    causes the subsequent input to be logged to the file with the name name-file, i.e., everything you type on your terminal will also appear in this file. Note that InputLogTo and LogTo cannot be used at the same time while InputLogTo and OutputLogTo can. Note that InputLogTo will overwrite the previous contents of this file if it already existed.


    InputLogTo()

    In this form InputLogTo stops logging.

  • OutputLogTo( name-file ) O

    causes the subsequent output to be logged to the file with the name name-file, i.e., everything GAP prints on your terminal will also appear in this file. Note that OutputLogTo and LogTo cannot be used at the same time while InputLogTo and OutputLogTo can. Note that OutputLogTo will overwrite the previous contents of this file if it already existed.


    OutputLogTo()

    In this form OutputLogTo stops logging.

    Note that one should be careful not to write to a logfile with PrintTo or AppendTo.

    When using

  • CrcFile( name-file ) F

    computes a checksum value for the file with filename name-file and returns this value as integer. The function returns fail if a system error occurred, say, for example, if name-file does not exist. In this case the function LastSystemError (see LastSystemError) can be used to get information about the error.

  • RemoveFile( name-file ) F

    will remove the file with filename name-file and returns true in case of success. The function returns fail if a system error occurred, for example, if your permissions do not allow the removal of name-file. In this case the function LastSystemError (see LastSystemError) can be used to get information about the error.

  • Reread( name-file ) F
  • REREADING

    In general, it is not possible to read the same GAP library file twice, or to read a compiled version after reading a GAP version, because crucial global variables are made read-only (see More about Global Variables) and filters and methods are added to global tables.

    A partial solution to this problem is provided by the function Reread (and related functions RereadLib etc.). Reread( name-file ) sets the global variable REREADING to true, reads the file named by name-file and then resets REREADING. Various system functions behave differently when REREADING is set to true. In particular, assignment to read-only global variables is permitted, calls to NewRepresentation (see NewRepresentation in ``Programming in GAP'') and NewInfoClass (see NewInfoClass) with parameters identical to those of an existing representation or info class will return the existing object, and methods installed with InstallMethod (see InstallMethod in ``Programming in GAP'') may sometimes displace existing methods.

    This function may not entirely produce the intended results, especially if what has changed is the super-representation of a representation or the requirements of a method. In these cases, it is necessary to restart GAP to read the modified file.

    An additional use of Reread is to load the compiled version of a file for which the GAP language version had previously been read (or perhaps was included in a saved workspace). See The Compiler and Saving and Loading a Workspace for more information.

    [Top] [Previous] [Up] [Index]

    GAP 4 manual
    February 2000