TweetFollow Us on Twitter

Disk Files
Volume Number:3
Issue Number:10
Column Tag:Basic School

Reading & Writing Disk Files

By Dave Kelly, MacTutor Editorial Board, Ontario, CA


Reading and writing data is often taken for granted. Yet we read and write data every time we turn on our computers. Fundamental to the Macintosh is the handling of graphics, sometimes in MacPaint format, and sometimes in DRAW or PICT format. Zedcor has some interesting commands that will speed up your data handling no matter which format you are using.

Fig. 1 Paint scroller program scrolls PICT images

The commands I am referring to are WRITE FILE (E-159) and READ FILE (E-126). I have used the examples in the manual as a base to set up my demo programs. Before I discuss this more, let’s review the syntax:

READ FILE [#] filenumber%, desinationAddress&, NumberofBytes&


WRITE FILE [#] filenumber%, desinationAddress&, NumberofBytes&

The key is to set up variables that you want to read or write, find the pointer to the variable (destinationAddress& ) and read or write the desired NumberofBytes&. Easy, right? I can just imagine some of the possibilities. How about a communications program? Or a file transfer program? Or how about writing your own minifinder?

The WRITE FILE example in the ZBasic manual demonstrates a way that variables may be temporarily stored (quickly) to disk, run another application, then return to this program and read the variables back via READ FILE. This could always be done before (with other Basic commands), but not as fast. I’ve taken the WRITE FILE example and modified slightly to create a Mini Finder type program. Those of you who have been around the Macintosh for a while will remember the old days when the minifinder option was not a part of the Finder. Several minifinder type programs popped up. By replacing the regular finder with a smaller finder, space on the disk can be preserved. That was really all you could do when disks were limited to 400K. Now with 800K disks and hard disks, the need is not as great. Still, with all the things being added to the system file, if you don’t have a hard disk your system disk is probably pretty full. My system file on my hard disk is over 1M byte (with all the fonts loaded of course).


First you must realize that this program will not work properly unless you compile it into a “full-blown” application. Since the program will launch another application it will be impossible to return to ZBasic and load you basic source code to run it again. So just compile as an application first. Also, this application must be located in the “blessed” folder (i.e. the system folder). The reason for this is that this application will temporarily take the place of the Finder (which also must remain in the system folder). In a sense, our ZBasic application becomes a system application.

The program makes use of two global variables (available to all Macintosh applications) namely, CurApName ($910) and FinderName ($2F0). The variable FinderName contains the name of the Finder application (usually “Finder”). CurApName contains the name of your application program. An application has no way to know if a user has changed its name. If the application needs to no its own name, it may refer to CurApName. The trick that this application uses is to temporarily change the name of the Finder application to the current application name. The CurApName and FinderName variables are stored in Pascal format with length first with the string following. Be careful to observe the proper length of the strings. CurApName is type Str31 which means it is a string with length (max) of 32. FinderName can only be 16 characters long because it is of type Str15. (A partial list of global variables is found on page E-198 in the ZBasic manual).

Dummy first and last variables are set up to determine the block of variables that you want to read. The variables will be read or write from that block of memory which is marked by the first and last variables. It is important to realize that ZBasic determines which variable is last during the compile process not during run time. This is the reason for the subroutine “Get Last Variable Pointer” at the end of the program. Notice that the last variable is defined at the very end of the program. If you only have a few variables to store you may want to position the first and last to include only those variables. Just remember that space should be reserved for reading variables back. Just for fun the program records the timer count as one of the variables to be stored. When the program is re-run after launching another application, it will check the timer again and display the elapsed time.

An important additional comment is that you should remember to tell the user what you have in mind when every you display a GetFile dialog box. IF your program displays a dialog box to load/save a file or as in this case to launch an application, be sure the the user knows why the dialog is displayed. If the dialog was displayed as the result of a menu that was selected then the purpose is self explanatory. Occasionally, I’ve seen programs that start up by displaying a GetFile dialog, without specifying that the application is looking for a paint type file (for example). Well, I don’t keep my paint files in the same folder as the applications so I wouldn’t know which type of file was being requested. My suggested solution is to display another dialog above or below the GetFile dialog which explains what kind of info is being requested. This is one way to make the program much more user usable.

The ZBasic Quick “ZFinder” demo may spark some more ideas to help your next programming project. The listing is included at the end of this column.


The next program was derived from the READ FILE demo on page E-126 of the ZBasic manual. Actually the only part of the demo that is used here is the method of reading a paint file. For more information on reading paint files I refer you to my column in September 1985 (gee, that was 2 years ago). Using READ FILE we read 720 lines with 72 bytes per line. The BLOCKMOVE statement is used to move the paint picture into a reserved space in memory. Some of the variables used in the 1st example on page E-126 are:

X%(1),71 -> represents a single line (72 chars) of the paint picture.
X$(719) ->  720 lines of the paint picture.
FL& ->  File length (does not include pattern header).

The toolbox routine UNPACKBITS is used to decompress the paint file a line at a time. The BLOCKMOVE statement moves the unpacked picture for safe keeping until the entire file is unpacked. Be sure that this routine is not used between Event ON statements and Event OFF as it will slow down a lot. That is because the Event statements cause the compiler to put GETNEXTEVENT calls at the beginning of each basic line. This routine works about 10 times faster without GETNEXTEVENT traps.

The are a few side effects of the Event loop which should be explained. For some reason the PUT statement didn’t work properly outside of the event loop. Originally, I had put the initial PUT statement in the subroutine that reads the paint file. This caused the picture to be erase by some refreshing of the output window. But when placed in the loop, the refreshing recognized that there was a change. I can’t explain why this happened. Perhaps, Andy or Michael Gariepy of Zedcor could explain this one sometime.

Credit for the cursors and scrolling go to Lee Bass of West Covina, CA. (Thanks Lee). Lee’s original program had no menu and no event loop. Really, the event loop should be short as possible (this one is kind of long), but since Lee implemented the mouse functions without an event loop that’s how it stayed. I would prefer that ON MOUSE be used to detect any mouse movements and remove all the appropriate statements from the event loop. If you are just learning you may want to try modifying the program by implementing ON MOUSE GOSUB type structure to the program. Lee’s cursors were created with ResEdit and converted to RMaker files to make it easier for you to type in these programs if you are so inclined. The window was divided in to thirds horizontally and thirds vertically to give 9 sections for the nine different cursors. During the main event loop the mouse is read to determine its position and the cursor is modified according to which of the window sections the coordinates fall. Pretty simple but maybe you have a better way. If you have any suggestions feel free to send them in.

This program requires that you use RMaker to create the cursor resources. Since the current version of RMaker doesn’t work well with HFS you should move the compiled application and RMaker out on to the desktop and run it from there. The RMaker source file can be in any folder. RMaker appends the cursor resources to the application file only if you have named the application the same as the name in the first line of the RMaker source file. If you can’t find RMaker (distributed with ZBasic) or don’t like it (or just plain don’t understand it) you may want to create your cursors with ResEdit. The cursor resource ID numbers are :

257-> up arrow
258-> down arrow
259-> left arrow
260-> right arrow
261-> top/left arrow
262-> top/right arrow
263-> bottom/right arrow
264-> bottom/left arrow
265-> center

To use ResEdit, create a new CURS resource for each ID number and either draw the cursor with its mask for each of the ID numbers above or use the RMaker listing to type data directly into the resource. Open the CURS resource as a general type and type the hex code directly into the window. You can check your work by opening the resource again as a cursor type and editing as needed. This is a good way to make your own custom cursors too. I recommend using resources whenever possible with your compiled applications because it makes it much easier to modify things later if you have to change anything. The cursor editor in ResEdit will do what you need.

Well, the column section is short this month, but you’ve got two programs to play with. Would you like to see a column about a particular subject? Just ask! Now that ZBasic is going strong there is such a large amount of material to discuss that it is difficult to know where to begin. Your preferences are welcome.

Other related MacTutor columns: “Random Access Files” Sept. 1986; “Reading Paint Files”, Sept. 1985; “On Fonts & Cursors”, Oct. 1986; “Reading MacPaint Files”, May 1987; “Scrolling in ZBasic”, April 1987. Some of these and others are also available in “Best of MacTutor, Vol. 1” book and “The Complete MacTutor, Vol. 2” book.

An unrelated note: Among the numerous bugs in the MS Compiler which have come upon us here is another interesting one:

INPUT;”Enter a number to print   : “,N
PRINT”   “;N

This program gives a system BOMB when the binary version is used. With the decimal version everything works fine. The output is:

Enter a number to print    :1E-3 1.000099E-03
Enter a number to print    :1E-4 .0001001
Enter a number to print    :1E-5 .0000101
Enter a number to print    :1E-6 .0000011
Enter a number to print    :1E-7 .0000001
Enter a number to print    :1E-8 BOMB!!!!

There has been no new response from Microsoft regarding the compiler. I still highly recommend ZBasic 4.0. Microsoft, are you listening??

REM ZBasic Quick Finder
REM ©MacTutor and Zedcor, 1987
REM Adapted from E-159 of ZBasic Manual
REM By Dave Kelly

REM This program allows you to Execute other applications
REM and return with all variables as they were

GOSUB “Get Last Variable pointer”

REM Check to see if returning from another application
 REM  If returning from an application
LONG IF LOF(1,1)>10
 REM  reload all the variables
 READ FILE #1,VARPTR(FirstVariable%), LastVariable&-VARPTR(FirstVariable%)
 TEXT 2,12,1
 PRINT”Last Application: “;:PRINT Application$
 PRINT”Elapsed Time: “;TIMER-StartTime&;” Seconds”
 TEXT 2,12,1
 PRINT SPC(5);”Welcome to Macintosh.”
 FOR I=1 TO SysLength
 SystemFinder$=SystemFinder$+ CHR$(PEEK(FinderName&+I))
 REM SystemFinder$ is the name of the original Startup appl.
PRINT”Please select an application to run.”

REM Moves This Applications filename to 
REM FinderName global variable
FOR I=0 TO 15: REM filename MUST be less than 16
 POKE FinderName&+I,PEEK(CurApName&+I)
REM *** Get name of application to execute
REM APPL=any executable file
REM Last variable used as dummy for READ and WRITE
LONG IF Application$=””
 REM Restore Finder filename as desktop start-up
 POKE FinderName&,SysLength
 FOR I=1 TO LEN(SystemFinder$)+1
 POKE FinderName&+I, PEEK(VARPTR(SystemFinder$)+I)
 RECORD#1,0,0:REM Reset file pointer to file begin
 REM *** Save Variables before executing Application
 WRITE FILE #1,VARPTR(FirstVariable%), LastVariable&-VARPTR(FirstVariable%)
 REM Execute Application now...
 RUN Application$,Volume%
“Get Last Variable pointer”

REM Paint Scroll Demo
REM ©Mactutor 1987
REM By Dave Kelly and Lee Bass
REM Thanks Lee!!
REM Adapted from E-126 of ZBasic manual

DIM X%(1),71 X$(719):X%(0)=576:X%(1)=720
ScrollInc=20:’Increment for moving picture
WINDOW #1,””,(50,80)-(430,310),3
GOSUB “OpenFile”

MENU 1,0,1,”File”
MENU 1,1,1,”Open /O”
MENU 1,2,1,”Quit/Q”
“Main Program Loop”
IF New=1 THEN PUT (-XChange,-YChange),X%(0), PSET:New=0
LONG IF Horizpos>0 AND Horizpos<380 AND Vertpos>0 AND Vertpos<230' in 
our window
 LONG IF Vertpos>153:REM bottom section
 IF Horizpos<127 THEN CursorNumber=264
 IF Horizpos>=127 AND Horizpos<=253 THEN                       CursorNumber=258
 IF Horizpos>253 THEN CursorNumber=263
 LONG IF Vertpos>=77 AND Vertpos<=153
 REM middle section
 IF Horizpos<127 THEN CursorNumber=259
 IF Horizpos>=127 AND Horizpos<=253 THEN                       CursorNumber=265
 IF Horizpos>253 THEN CursorNumber=260
 LONG IF Vertpos<77:REM top section
 IF Horizpos<127 THEN CursorNumber=261
 IF Horizpos>=127 AND Horizpos<=253 THEN                       CursorNumber=257
 IF Horizpos>253 THEN CursorNumber=262
 LONG IF MouseEvent<0
REM cursor down change xx,yy to move picture
 IF CursorNumber=259 THEN XChange=XChange-ScrollInc
 IF CursorNumber=260 THEN XChange=XChange+ScrollInc
 IF CursorNumber=257 THEN YChange=YChange-ScrollInc
 IF CursorNumber=258 THEN YChange=YChange+ScrollInc
 IF CursorNumber=261 THEN XChange=XChange-                     ScrollInc:YChange=YChange-ScrollInc
 IF CursorNumber=262 THEN XChange=XChange+ScrollInc:YChange=YChange-ScrollInc
 IF CursorNumber=263 THEN XChange=XChange+ScrollInc:YChange=YChange+ScrollInc
 IF CursorNumber=264 THEN XChange=XChange-                     ScrollInc:YChange 
= YChange+ScrollInc
 IF YChange<0 THEN YChange=0
 IF XChange<0 THEN XChange=0
 IF YChange>720-230 THEN YChange=720-230
 IF XChange>576-380 THEN XChange=576-380
 PUT (-XChange,-YChange),X%(0),PSET
 CURSOR MyCursor
 CURSOR 0:CursorNumber=0:REM Not in our window
UNTIL Finished=1

IF Menuitem=1 THEN GOSUB “OpenFile”
IF Menuitem=2 THEN END

TEXT 2,12,1
PRINT SPC(5);”Please Select a Paint File to View”;
“Load PaintPic”
PRINT “Now Loading “;F$;
REM Read in the Paint File
OPEN”I”,1,F$,1,V% : S&=LOF(1):REM Set length of Paint File
FL&=S&-512:CURSOR 4:RECORD #1,512
FOR I = 1 TO 720:REM Lines in the Paint Picture
 LONG IF N>180
 BLOCKMOVE X&,A&,256-N:X&=A&
 READ FILE #1,A&+256-N,NX:FL&=FL&-NX

;RMaker File for Paint Scroll Program

!Paint Scroll


0080 0140 0220 0410 0808 1004 2002 7E3F 0220 0220 0220 0220 0220 0220 
0220 03E0 0080 01C0 03E0 07F0 0FF8 1FFC 3FFE 7FFF 03E0 03E0 03E0 03E0 
03E0 03E0 03E0 03E0 0000 0008
03E0 0220 0220 0220 0220 0220 0220 0220 7E3F 2002 1004 0808 0410 0220 
0140 0080 03E0 03E0 03E0 03E0 03E0 03E0 03E0 03E0 7FFF 3FFE 1FFC 0FF8 
07F0 03E0 01C0 0080 000F 0008
0100 0300 0500 0900 1100 21FF 4001 8001 4001 21FF 1100 0900 0500 0300 
0100 0000 0100 0300 0700 0F00 1F00 3FFF 7FFF FFFF 7FFF 3FFF 1F00 0F00 
0700 0300 0100 0000 0007 0000
0080 00C0 00A0 0090 0088 FF84 8002 8001 8002 FF84 0088 0090 00A0 00C0 
0080 0000 0080 00C0 00E0 00F0 00F8 FFFC FFFE FFFF FFFE FFFC 00F8 00F0 
00E0 00C0 0080 0000 0007 000F
FFC0 8080 8100 8200 8100 8080 9040 A820 C410 8220 0140 0080 0000 0000 
0000 0000 FFC0 FF80 FF00 FE00 FF00 FF80 FFC0 EFE0 C7F0 83E0 01C0 0080 
0000 0000 0000 0000 0000 0000
07FE 0202 0102 0082 0102 0202 0412 082A 1046 0882 0500 0200 0000 0000 
0000 0000 07FE 03FE 01FE 00FE 01FE 03FE 07FE 0FEE 1FC6 0F82 0700 0200 
0000 0000 0000 0000 0000 000E
0000 0000 0000 0000 0200 0500 0882 1046 082A 0412 0202 0102 0082 0102 
0202 07FE 0000 0000 0000 0000 0200 0700 0F82 1FC6 0FEE 07FE 03FE 01FE 
00FE 01FE 03FE 07FE 000F 000F
0000 0000 0000 0000 0080 0140 8220 C410 A820 9040 8080 8100 8200 8100 
8080 FFC0 0000 0000 0000 0000 0080 01C0 83E0 C7F0 EFE0 FFC0 FF80 FF00 
FE00 FF00 FF80 FFC0 000F 0000
0000 0080 01C0 03E0 0080 0080 1004 3086 7DDF 3086 1004 0080 0080 03E0 
01C0 0080 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 
0000 0000 0000 0000 0008 0008

Community Search:
MacTech Search:

Software Updates via MacUpdate

World of Tanks Generals guide - Tips and...
World of Tanks Generals is a brand new card game by the developer behind the World of Tanks shooter franchise. It plays like a cross between chess and your typical card game. You have to keep in consideration where you place your tanks on the board... | Read more »
TruckSimulation 16 guide: How to succeed...
Remember those strangely enjoyable truck missions in Grand Theft Auto V whereit was a disturbing amount of fun to deliver cargo? TruckSimulation 16 is reminiscent of that, and has you play the role of a truck driver who has to deliver various... | Read more »
The best GIF making apps
Animated GIFs have exploded in popularity recently which is likely thanks to a combination of Tumblr, our shorter attention spans, and the simple fact they’re a lot of fun. [Read more] | Read more »
The best remote desktop apps for iOS
We've been sifting through the App Store to find the best ways to do computer tasks on a tablet. That gave us a thought - what if we could just do computer tasks from our tablets? Here's a list of the best remote desktop apps to help you use your... | Read more »
Warhammer 40,000: Freeblade guide - How...
Warhammer 40,000: Freebladejust launched in the App Store and it lets you live your childhood dream of blowing up and slashing a bunch of enemies as a massive, hulking Space Marine. It's not easy being a Space Marine though - and particularly if... | Read more »
Gopogo guide - How to bounce like the be...
Nitrome just launched a new game and, as to be expected, it's a lot of addictive fun. It's called Gopogo, and it challenges you to hoparound a bunch of platforms, avoiding enemies and picking up shiny stuff. It's not easy though - just like the... | Read more »
Sago Mini Superhero (Education)
Sago Mini Superhero 1.0 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0 (iTunes) Description: KAPOW! Jack the rabbit bursts into the sky as the Sago Mini Superhero! Fly with Jack as he lifts impossible weights,... | Read more »
Star Wars: Galaxy of Heroes guide - How...
Star Wars: Galaxy of Heroes is all about collecting heroes, powering them up, and using them together to defeat your foes. It's pretty straightforward stuff for the most part, but increasing your characters' stats can be a bit confusing because it... | Read more »
The best cooking apps (just in time for...
It’s that time of year again, where you’ll be gathering around the dinner table with your family and a huge feast in front of you. [Read more] | Read more »
Square Rave guide - How to grab those te...
Square Rave is an awesome little music-oriented puzzle game that smacks of games like Lumines, but with its own unique sense of gameplay. To help wrap your head around the game, keep the following tips and tricks in mind. [Read more] | Read more »

Price Scanner via

13-inch 128GB MacBook Air now on sale for $79...
Best Buy has just lowered their price on the 2015 13″ 1.6GHz/128GB MacBook Air to $799.99 on their online store for Cyber Monday. Choose free shipping or free local store pickup (if available). Sale... Read more
Best Buy lowers 13-inch MacBook Pro prices, n...
Best Buy has lowered prices on select 13″ MacBook Pros this afternoon. Now save up to $200 off MSRP for Cyber Monday on the following models. Choose free shipping or free local store pickup (if... Read more
Cyber Monday: Apple MacBooks on sale for up t...
Apple resellers have MacBook Pros, MacBook Airs, and MacBooks on sale for up to $250 off MSRP for Cyber Monday 2015. The following is a roundup of the lowest prices available for new models from any... Read more
Cyber Monday: Apple Watch on sale for up to $...
B&H Photo has the Apple Watch on sale for Cyber Monday for $50-$100 off MSRP. Shipping is free, and B&H charges NY sales tax only: - Apple Watch Sport: $50 off - Apple Watch: $50-$100 off B... Read more
Cyber Monday: 15% off Apple products, and sto...
Use code CYBER15 on Cyber Monday only to take 15% on Apple products at Target, and store-wide. Choose free shipping or free local store pickup (if available). Sale prices for online orders only, in-... Read more
iPad Air 2 And iPad mini Among Top Five Black...
Adobe has released its 2015 online shopping data for Black Friday and Thanksgiving Day. The five best selling electronic products on Black Friday were Samsung 4K TVs, Apple iPad Air 2, Microsoft Xbox... Read more
All-in-one PC Shipments Projected To Drop Ove...
Digitimes’ Aaron Lee and Joseph Tsai report that all-in-one (AIO) PC shipments may drop a double-digit percentage on-year in 2015 due to weaker-than-expected demand, although second-largest AIO make... Read more
Sprint Offers iPad Pro
Sprint now offers Apple’s new iPad Pro with Wi-Fi + Cellular, featuring a 12.9-inch Retina display with 5.6 million pixels. Customers can pick up iPad Pro at select Sprint retail locations. It can... Read more
Cyber Monday: Target offers 15% discount on A...
Target has discounted Apple Watches by 15% for Cyber Monday. Choose free shipping or free local store pickup (if available). Sale prices for online orders only, in-store prices may vary: - Apple... Read more
Sunday roundup of Holiday weekend Mac sales:...
Take up to $500 off MSRP on the price of a new Mac at B&H Photo today as part of their Black Friday/Holiday weekend sale. Shipping is free, and B&H charges NY tax only. These prices are... Read more

Jobs Board

*Apple* Site Security Manager - Apple (Unite...
# Apple Site Security Manager Job Number: 42975010 Culver City, Califo ia, United States Posted: Oct. 2, 2015 Weekly Hours: 40.00 **Job Summary** The Apple Site Read more
iOS Wallet & *Apple* Pay Engineer - App...
# iOS Wallet & Apple Pay Engineer Job Number: 40586801 Santa Clara Valley, Califo ia, United States Posted: Nov. 16, 2015 Weekly Hours: 40.00 **Job Summary** The iOS Read more
*Apple* Online Store Expansion - Apple (Unit...
# Apple Online Store Expansion Job Number: 41191932 Santa…1, 2015 Weekly Hours: 40.00 **Job Summary** The Online Apple Store is seeking a person to lead its Read more
Software Engineer, *Apple* Watch - Apple (U...
# Software Engineer, Apple Watch Job Number: 38181776 Santa Clara Valley, Califo ia, United States Posted: Nov. 2, 2015 Weekly Hours: 40.00 **Job Summary** The Apple Read more
Technical Program Manager, Strategic Merchant...
# Technical Program Manager, Strategic Merchants - Apple Pay Job Number: 44001177 Santa Clara Valley, Califo ia, United States Posted: Oct. 30, 2015 Weekly Hours: 40.00 Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.