home *** CD-ROM | disk | FTP | other *** search
- EDIT RANDOM FILES
-
- by Richard Bain
-
- Edit'wheel'data was written as a
- companion program to the
- wheel'of'fortune game. With a few
- small changes, it can be used to edit
- other random files with one string
- per record.
-
- When RUN, it first asks for a
- filename. The default prompt,
- ran.wheel, is the file used with the
- wheel'of'fortune game (see page 56).
- The file is then opened:
-
- open file 2,filename$+",r",random 41
-
- The desired file may not be on the
- disk because the wrong disk is in the
- drive or the filename was typed
- incorrectly. Normally, when COMAL
- tries to OPEN a RANDOM file that is
- not on the disk, it will create a new
- file entry. This is often
- undesirable. It is important to check
- if the file exists before trying to
- open the file, or include +",r". Then
- the disk drive will respond with a
- file not found error rather than
- create a new file entry. This is a
- faster method to open an existing
- file because the file is only opened
- once, not twice as with the
- file'exists method. A second
- advantage to this method is that the
- disk drive will give a file type
- mismatch error if the file is on the
- disk, but is not a RANDOM file. A
- disadvantage is that it is not
- transportable to other versions of
- COMAL.
-
- Random 41 specifies that the file
- record length is 41 bytes. This
- allows for 39 characters plus 2 bytes
- for the character count as used by
- ran.wheel. Other files may need a
- different record length. The maximum
- possible length is 254.
-
- Edit'wheel'data then reads in the
- entire file. The file contains a
- special format to make this easy. The
- first record in the file contains,
- max'used, the number of the last
- valid record. Once max'used is
- determined, records 2 through
- max'used can safely be READ. The
- program must keep track of the number
- of records and WRITE it to the first
- record prior to closing the file.
- This can serve as a useful standard
- for all RANDOM files.
-
- The program then asks if you want to
- edit the data. This option lets you
- change any existing record. The
- editing process is easy. The old
- entry is printed on the screen and
- the cursor is placed on the first
- character. Press <RETURN> to keep it
- unchanged, or type in the changes you
- desire. Note that strings are limited
- to 39 characters.
-
- After you are finished editing the
- data, you have the option of adding
- more data (up to 100 records). Type
- in the names and phrases you want.
- When you have entered all the data,
- hit <RETURN> on a blank line to tell
- the computer you are finished.
-
- Next, you will be asked if you want
- any more changes. I recommend you say
- yes. This will let you edit the data
- again. Even if you think it is right,
- this option lets know before it is
- saved.
-
- When the data is the way you like it,
- you will be asked if you want to save
- it. All changes have been made in
- computer memory only, so you almost
- certainly will want to save it back
- to the disk. The default filename is
- the one specified at the start of the
- program, but can be changed. If you
- specify a filename that is not on the
- disk, a new file will be created.
- [Note: it is possible to swap disks
- during the program, so this option
- can be used to copy the file from one
- disk to another. This may be
- important for those of you who don't
- have a RANDOM file copy program.] The
- records are written to the file in
- reverse order. When creating a new
- RANDOM file, or appending to an old
- one, it is important to WRITE the
- last record before the others. Then
- the file only needs to allocate new
- records once instead of allocating
- each record as it is used.
-
- Random File Problems
-
- It seems that if you PRINT FILE a
- number to a RANDOM file in COMAL
- 0.14, the record entry will start
- with CHR$(13) and then be padded with
- CHR$(0)s. In COMAL 2.0, the number is
- placed in the record, but the rest of
- the record is still padded with
- CHR$(0)s, overwriting anything which
- may have been there before. The
- problems seem to disappear when using
- READ FILE and WRITE FILE.
-
- The 1541 has known bugs writing to
- RANDOM files. I try to avoid them by
- reading back each record right after
- it is written. This seems to solve
- the problem. If it doesn't, at least
- the program stops when the error
- occurs.
-