Filenames

We've seen how to create a file ane read it back before, but this program is slightly different. This prompts the user for a filename and then creates the file you choose. A standard dialog is used with file, folder and drive prompts.

A variable, a$, is used to store the filename and path, and we can set this to a default name that will be displayed in the dialog with a$="c:\filename". The dialog is straightforward: dInit "New File",16 sets the title - the 16 means use a compact display. The file controls are added with dFile a$,"File,Folder,Disk",1. The File,FolderDisk are the prompts and the 1 means use an edit box. Other values include: 0 (use a selector), 2 (allow directory names), 4 (directory names only).

Two buttons are used in the dialog - Cancel and OK - using dButtons. The button captions are followed by the values of the keys to respond to - 27 (Escape) and 13 (Enter). The $100 added to them tells the system not to display shortcuts under the buttons Take out the $100 and see what happens.

There's a Trap Delete a$ line, which we've seen before. This deletes the file before trying to create it, otherwise the program would stop if a file of the same name existed. In a real program you would probably check to see if the file existed and ask whether the user wanted to overwrite that file.

Another interesting point is the a$=chr$(32)+a$+chr$(34) line. The reason for this is that you can't use Create to create a file if there are spaces in the filename. However, if you put the filename in quotes like this "My File" rather than My File then everything is OK. Chr$(34) is the quote symbol ". Try taking out this line and see what happens when you enter a filename with spaces in.

PROC Filenames:
Local a$(255)

a$="c:\filename"
dInit "New file",16
dFile a$,"File,Folder,Disk",1
dButtons "Cancel",27+$100,"OK",13+$100
If Dialog<>13 : Stop : Endif

Trap Delete a$

Print "Creating file..."
a$=chr$(34)+a$+chr$(34)
create a$,A,b$,c$
A.b$="2"
A.c$="3"
Append
Close

Print "Reading file..."
Open a$,A,d$,e$
Print A.d$,A.e$
Close
Get

ENDP
 

RAW Computing home page

  Psion home page