External Files in HyperTalk

Syntax of some commands you will need:

open file filename
close file filename
write data to file filename
read from file filename for number
	  or
read from file filename until character

Accessing Files

  1. Opening Files:

    In order to write to or read from a file, the file must first be opened. Following is an example of the open command:

    open file "My disk:binary data"

    This command will open a file called "binary data" on a floppy disk named "My disk". If such a file doesn't already exist HyperCard will create it. If HyperCard has problems opening the file it will put an error message into the variable result. Whenever you try to open a file you should test whether the variable result is empty. If so, then the open was successful. If not, you should print the error message and stop the program. For example:

         on myHandler
             open file "My disk:binary data"
    
             if the result is not empty then
                 -- error opening file: 
                 -- display the error message and leave this handler
                 answer the result with "okay"
             else
                 -- file opened successfully: 
                 -- what follows is the rest of the script for myHandler
                          ...
    
                 close file "My disk:binary data"
             end if
         end myHandler
    
    (Note that the name myHandler is just an example name. Select names for your handlers that describe the function they perform.)

  2. Closing Files:

    You should always close any open files before the end of your script. If you fail to do this, other handlers will not be able to read from or write to the file. If you fail to do this, other handlers will not be able to read from or write to the file. An example:

    close file "My disk:binary data"

    This command will close the file "My disk:binary data".


Writing Data to a File

A file must be opened before it can be written to. To check whether the write command has worked:

  1. Locate the data file
  2. Double click on the data file's icon
  3. Close the data file's window before proceeding.

Following are several examples of the write command:

write card field "binary field" to file "My disk:binary data"

This command will write the contents of the field "binary field" to the named file.

write word X of line Y of card field "text field" & " " to file "My disk:binary data"

This command will write the Xth word of the Yth line of the card field to the file "My disk:binary data". Note that a blank has been explicitly added to separate words in the data file.


Reading Data From a File

A file must be opened before attempting to read from that file. When a file is opened, not only do you gain access to the file, but HyperCard also maintains the position in the file. This position indicates where the next read from the file (or write to the file) will commence. Initially, this position is the first character in the file. "Read from fileName for 8" not only copies (up to) 8 characters from the file fileName into the container it, but also advances the position 8 characters down the file, so that the next read will not be copying from the same location in the file. If the position is at the end of the file, so that no more characters can be read from fileName then "read from fileName for 8" results in it being empty.

There are two general forms of the read command:

  1. read from file filename for number

    This is appropriate if you want to read the whole file at once, or if you know exactly how many characters you want to have read at a time. For example:

    read from file "My disk:binary data" for 50

    This command will read the next 50 characters from the named file (or the whole file, if there are fewer than 50 characters remaining) and puts the characters into the variable it.

  2. read from file filename until character

    This form of the command is appropriate when you want to read a chunk of data a time, such as a word, item or line. You can also read everything from the file up until any character desired. Note that the character used to read to is included in the data read from the file. Two examples follow:

    read from file "My disk:binary data" until return

    This command will read the next line of data from the named data file. The line of data is put into the variable it.

    read from file "My disk:binary data" until " "

    This command will read the next word of data from the named data file. The word of data is put into the variable it.


General Observations

  1. You need to take care that your "read-from-file" process is consistent with your "write-to-file" process. That is, if you wrote separating spaces to the file, don't read the spaces as part of your 8-bit word.

  2. The filename in quotes must be exactly the same as the actual filename including the title of the diskette where the file is located. Even leading or trailing blanks which occur in the filename or diskette title must be included in the quoted filename. If you continue to get errors when attempting to open a file, check for the exact spelling of the diskette title and the data file name.

  3. One way to simplify using external file names is to put them in a variable and then reference the variable name as needed. For example:

      on fileExample
           put "My disk:binary data" into extfile
           open file extfile
           if the result is empty then
    	          write card field X to file extfile
                 close file extfile
           end if
      end fileExample