Assignment for next lab:
- Read pp. 214-228
- Turn in a final project draft proposal for your team. (see handout on
Final Projects)
CS10 Fall 1997
Homework 4
Due Date: Tues. Nov. 4
Assignment Description
Your assignment is to complete lab exercise 3 on pages 202 - 205. Specifically,
you are asked to make the following changes to the Howard Stone stack:
- Modify the "translate" handler (found in the background script) so that it saves
the binary string displayed in the card field "Binary Field" to an external file called "Binary Data."
(exercise 3.1 on p. 203)
- Create a button called "read from disk" which will do the following:
- a. Read the file "Binary Data" from a disk and display its contents in
the card field "Binary Field."
(exercise 3.2 on p. 204)
- b. Translate the binary numbers contained in the file "Binary Data"
into their ASCII (decimal) equivalents and display these values in card field
"ASCII Field." (exercise 3.3 on p. 204)
- c. Translate the numbers in the card field "ASCII Field" to their character equivalents and display
these characters in the card field "Text Field." (exercise 3.3 on
p. 204)
- d. Interpret the contents of card field "Text Field"
as a Hypertalk instruction. (exercise 3.4 on p. 205)
You should define a handler for each item a-d above (put these handlers somewhere above the "read from disk"
button in the object hierarchy). Then have the mouseUp handler of your "read from disk" button use the
handlers which you wrote.
Your handler for item b above should use your makeDecimal function from Lab 9, and your handler for
item c above should use the Hypertalk numToChar function.
Use the remaining time in lab today to get started on this homework. Begin by finishing your makeDecimal
function (from Lab 9) if you haven't already. Remember that you can test this function from the
message box.
Once you have your makeDecimal function working, complete item 1 above, and then write handlers
for items a-d above. Test and debug each handler before moving on to the next one. Handlers can be
tested from the message box.
The idea is to do the problem in stages and to make sure each stage is working
properly before proceeding to the next stage. This is good software engineering
practice.
Remember to
document all modifications you make by including comments (lines beginning with --) in your
scripts.
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
- 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.
- 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. 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:
- Locate the data file
- Double click on the data file's icon
- 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:
- 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 .
- read from file filename until character
This form of the command is appropriate when you want to read a chunk of data
at 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. Example:
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.