Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous contents index
Next: User Interaction Up: Input and Output Previous: Input and Output

Subsections


ASCII I/O

ASCII files combined with regular expressions (See § 4.9, page [*]) are the most flexible I/O mechanism in Glish.

Opening Files

The open function is used to open files in a Glish script. This function is modeled upon the open function in Perl. open is passed one string. This string describes both the file name and how the file should be opened. To open a file for reading, you would do something like:

    x := open("< /etc/passwd")
This would open the file /etc/passwd for reading. UNIX shell style redirection is used within the string to indicate how the file is to be opened. In a similar manner, a file would be opened for writing like:
    y := open("> /tmp/foo")
Opening a file this way will result in any contents in the file being overwritten. If instead, you want to append to an existing file, you would open the file like:
    y := open(">> /tmp/foo")
Output can also be piped through a process, and input can be generated by a process. Here are some examples:
    z1 := open("| cat -n > /tmp/bar")
    z2 := open("cat -n /etc/passwd |")
In the case of z1, the output written to z1 first goes through the cat process, and then it is written to the file /tmp/bar. cat is used to create the input to be read from z2. Finally, it is also possible to open a file for writing to a process, and then read the output of the process. Here is an example:
    z3 := open("| cat -n | tee /tmp/out |")
This example sends the output written to z3 through two processes, i.e. cat and tee. The output of tee provides the input to be read from z3. This sort of writing and reading from a process must be used very carefully because it relies on buffering by the operating system, and there can be problems if the buffers fill up.

The is_file function can be used to check if a variable is a file handle or not.

Reading

Once a file has been opened for reading, there are two different ways to read the information from the file. The first is to use the read function. By default, this function reads one line from a file (up to and including the newline). It returns a zero length string when the end of the file is reached. So if you wanted to print out the /etc/passwd file, you could do something like:
    x := open("< /etc/passwd")
    while ( line := read(x) ) print line
read can also be used to read a specified number of characters from a file, for example:
    x := open("< /etc/passwd")
    print read(x,40,'c')
would print out something like:
    root:x:0:1:Super-User:/:/sbin/sh
    daemon:
Here forty characters were read from the file. Newlines are included, but treated no different than any other character. If you want to read bytes instead of characters, use a 'b' parameter instead of a 'c' parameter. In this case, an array of bytes will be returned.

In addition, there is a special syntax (similar to that provided by Perl) for reading files. Using this syntax, a file can be read like:

    x := open("< /etc/passwd")
    while ( <x> ) print _
The angle brackets, e.g. <x>, perform an operation equivalent to:
    _ := read(x)
The underscore is special because it is the default variable for regular expression application. If you tried either this example or the last, you will have noticed that there is an extra newline for each line printed. This is because each line read from the file has an embedded newline character, and the print statement adds another newline. The last example could be rewritten to chop off the embedded newline as:
    x := open("< /etc/passwd")
    while ( <x> ) print ~ s/\n$//
Notice that this uses the unary application of a regular expression (using the underscore variable) to chop off the trailing newline. This loop is exactly equivalent to:
    while ( _ := read(x) ) print _ ~ s/\n$//

Writing

Writing to a file can be accomplished with either the standard write function or the UNIX stdio.h styled fprintf.

The write function simply converts one or more values to strings and writes them to a file, for example:

    x := open("| cat -n |")
    write(x,[a=1:3, b=3.6+1.06i])
    print read(x)
would print:
         1  [a=[1 2 3] , b=3.6+1.06i]
There is an an optional separator value (sep) which can be used to set the separator between values like:
    write( x, 1, 2, [a=1:3, b=3.6+1.06i], 3, 4, sep='@' )
    write( x )
    print read(x)
would print:
         2  1@2@[a=[1 2 3] , b=3.6+1.06i]@3@4@
The extra write(x) is important because it writes out a newline. Without it, the read would hang because it reads until a newline is encountered. The first write changed the separator from the default '\n' to @ so no newline was written as part of the initial write.

fprintf is also available for writing to a file. Here is an example:

    fprintf(x,'%x ',76:83)
write(x)
    print read(x)
would print:
         3  4c 4d 4e 4f 50 51 52 53
(See § 10.5.2, page [*] for a discussion of all of the options available with fprintf.)


next up previous contents index
Next: User Interaction Up: Input and Output Previous: Input and Output   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15