Text Files and the FileIO Xtra
by Gary Rosenzweig
|
This week, to celebrate (and shamelessly promote) my new book, I've decided to give you a useful excerpt from it. The new book is Special Edition Using Director 8 from QUE. It is primarily an update of my Special Edition Using Director 7 book, but I worked hard to add Director 8's new features to the book, improve each and every chapter, and add all sorts of new goodies. This is the section from chapter 16 that deals with the FileIO Xtra. It gives two very useful handlers that allow you to deal with reading from and writing to text files. I think it is useful in its own right, as well as a good representation of the contents of the book. Hope you like it, and I hope you'll check out the book at your local bookseller or online. Check out http://clevermedia.com/resources/bookstore/book5.html for details and a table of contents. Using text files has always been more difficult in Director than it should be. Director 8 is no different. It requires you to use the FileIO Xtra. This is an Xtra that adds Lingo commands to handle reading and writing files. Using an Xtra such as this is like using a behavior, except that the Xtra is referenced through a variable, not a sprite. Here is an example. To read a text file, use the following series of commands:
fileObj = new(xtra "FileIO") The variable fileObj is used to store an instance of the Xtra. The new command in the first line creates this instance, and a pointer to it is placed in the variable fileObj. You can think of fileObj as the me in a behavior. It is now needed to refer to this instance of FileIO. An instance of FileIO is capable of opening, creating, writing, reading, and deleting files. In the preceding example, the object is used to open a file with the openFile command, read the contents of the file with the readFile command, and then close the file with the closeFile command. The 1 at the end of the openFile command signifies that the file is being opened for reading. A 2 would mean that it is opened for writing. Here is a complete list of commands used with the FileIO Xtra:
Reading and writing files takes many lines of code. However, a handler that does this can be reused many times. Here is a handler that prompts users for a text file to read, and then returns that text file's contents:
on openAndReadText
-- create the FileIO instance
-- set the filter mask to text files
-- open dialog box
-- check to see if cancel was hit
-- open the file
-- check to see if file opened ok
-- read the file
-- close the file
--return the text
end If you want to use this handler to read a file that you already know the name of, just pass in the filename variable as a parameter to the handler, and remove the references to the setFilterMask and displayOpen commands. The opposite of this handler is one that saves any text to a file. This handler takes care of it all, down to setting the file type of the new file to a SimpleText file for the Mac. The text placed in the file is passed in as a parameter. Oddly, when writing out a new file, you must first open the file, and then use the delete command to delete it. Then, you can use createFile and openFile to create and write a new file. If you do not do this, the new file overwrites any file of the same name that already exists, and there might be some parts of the old file left in the new one.
on saveText text
-- create the FileIO instance
-- set the filter mask to text files
-- save dialog box
-- check to see if cancel was hit
-- delete existing file, if any
-- create and open the file
-- check to see if file opened ok
-- write the file
-- set the file type
-- close the file
return TRUE
end The on saveText and on openAndReadText handlers can be customized in many ways to suit many purposes. You can use getOSDirectory(), for instance, to find the path to the operating system and store files in the preferences folder. You can also use the Lingo property the pathname to get the path of the Director application or Projector to store files there.
|