Classic Computer Magazine Archive COMPUTE! ISSUE 26 / JULY 1982 / PAGE 97

A Direct Access File Editor

Charles Brannon
Editorial Assistant

For PET/CBM or Atari disk users, any memory size. This is a handy program if you ever need to update files.

What's in a file? A payroll program might keep a list of employees, social security numbers, status, etc., on a disk file. A check balancing program would probably save the information gathered during the run of the program such as check number, payee, and amount paid on a file.

Most files are very similar – a list of numbers and strings (words, names, etc.). Each line of the file is "sent" to the file with some variant of the PRINT statement, such as PRINT #1,A$ or PRINT #1, ZX(I,J). [PRINT #1 ; A$ on Atari.] Each line (also called a field) ends with a carriage return.

This carriage return is used to separate each line in the file. Otherwise, the file would be just a long block of characters, each line running into the next. These separators are called delimiters. On many computers, the comma and colon can also be used (but can cause trouble) as delimiters. This is important, as we will see later.

A File Editing Tool

You probably use files every day, even if you're not a programmer. If you use any commercial software such as a word processor, or a prepackaged accounting program, you are unknowingly generating files. Such software writes files to the disk to save the results of calculations and your input, then later reads the file, when you run the program anew, to "restore" the original conditions and information.

Unfortunately, few programs let you edit this information (excepting a word processor, of course). You may have a check-balancing program that writes to disk all checks that you enter in order to use this information for taxes at the end of the year. This file simply accumulates information. If you later discover that you've made a mistake on a check entered, there is no easy way to find or correct that check. What you need is a program that can read the entire file into memory, let you view it and make selective changes, then rewrite the corrected file back out to the disk.

The program here is a file editor. Basically, it asks you for the name of the file you want to edit, reads it in, and lets you "page" through the file 19 lines at a time. You can edit, replace, insert, and delete lines, as well as search the file for any sequence of characters.

This program only works with a file organized in the common "list" structure previously mentioned (which limits it to sequential files on the PET/CBM).

The two versions presented here, for the Atari and the PET/CBM, are different, but they both perform similar functions. After you enter the file name, the file is read into a string array. If the file is too large to fit into memory, the message "FILE TOO LONG" will appear. You can change the variable MAX (maximum number of lines in file) if you have enough memory. The file is displayed as numbered lines in the top 19 lines of the screen. Your input is taken at the bottom. When prompted for a line, always enter the number of the line.


If you enter any command that you don't want to perform (such as Replace when you meant to type Edit), you can usually exit the command with a null input (just press <RETURN>) on the PET/CBM, or by pressing the ESC (escape) key on the Atari.

Since commas and colons are used as delimiters on the PET/CBM, you can't enter or edit a line containing commas. To enter such a line, hold down SHIFT when you press the command key such as SHIFT-E for Edit, SHIFT-I for Insert, etc. A quote will automatically be printed at the start of the line, permitting you to enter commas and colons without the infamous ?EXTRA IGNORED message. After you enter the line, all commas (CHR$(44)'s) are converted to "fake commas" that look like commas, but don't work as delimiters (CHR$(108)).

Create A "Fake File"

You could even use this program to create files if you can create a "fake file" containing only one item such as:

OPEN  1, 8, 8, "0 : filename" (on the PET)
PRINT# 1, "X" : CLOSE 1
OPEN# 1, 8, 0, "D : filename" (Atari)
PRINT# 1 ; "X" : CLOSE# 1

You can then Replace the first line (the "X"), and use Append to add to the file.

I'll leave you with one important reminder: remember to re-SAVE a file after you edit it or your changes will have been in vain.

Editing Commands

PET/CBM Atari Command Description
<RETURN> N Next page Displays the next 19 lines.
E E Edit The primary "change" command. You enter the number of the line to be changed. The line is displayed at the bottom of the screen with the cursor on the first character. You can use your computer's editing keys to change the line (just like in an INPUT statement). Press <RETURN> when finished.
R R Replace You just enter a completely new line to replace any line in the file.
I I Insert Line For Atari: you enter the number of the line you want to insert a new line at. That line and all following lines are "pushed down," and the word "*** INSERT ***" is inserted. You can then use Replace to add a new line. For PET/CBM: you enter the number of the line you want to insert a line at, and then you enter the line you want inserted.
D D Delete Line Enter the number of the line to be deleted. The following lines will be "pulled up" to fill the gap. For PET/CBM: If you use SHIFT-D (shifted D), you can enter the starting and ending lines of a block of lines you'd like deleted.
A A Append Line After you type "A" you can enter a new line which will be added to the end of the file.
P P Print Enter the starting and ending lines of the block of lines you would like printed to the printer. For the PET/CBM, this is device number four. You can just hit < RETURN > to both prompts to print the entire tile (Atari version).
S W Save/Write Stores the file on disk. PET/CBM: scratches the old file, then writes the new file. Atari: Replaces the old file, unless you save the file under another name.
X Q Exit/Quit Lets you end the program.
0/1 [START] Re-run Atari version: If you press the START key at the prompt "Which?" you can RUN the program over again. PET/CBM version: Entering zero or one specifies which drive to use for the next file you want to edit. You can then enter the new file name.
G G Go to line Lets you "skip around" in the file. Enter the number of the line you want to see.
n/a O Other menu There are two menus in the Atari version, since the bottom of the screen can only hold so many commands. The other menu contains the commands Length, Search, and Next Search. You can add your own commands to this menu.
H n/a Help Unlike the Atari version, the PET/CBM version doesn't constantly display all choices. Just type H to see a list of all options.
K n/a Kill File Lets you scratch a file from within the program.
Z n/a Flips the character set.
Find/Search Quick Find Next Search

*Use (O)ther Menu
You enter a series of characters you want to find in the file. In the PET/CBM version, all matching lines are displayed. Press any key to abort the search. The Atari version only displays the first occurrence of the match. The Next Search command is used to find the next occurrence of the same search string. You can search either by character or by line. The character search will search the entire file character-by-character for the string, where the line search only matches the search string with the beginning of the line, and is naturally faster. The PET/CBM version has a separate command, Quick Find, to search by line.
L L Length Displays the number of lines in the file.