Working with Folders and Files Generally speaking, there are only 2 types of files: Text and Binary. Computers only work with zeros (0) and ones (1) also known as the Binary System. So every letter (A-Z, a-z), number (0-9) and symbol (+,-,/,$,# etc) is represented by a different binary number. Look up an ASCII code table ( http://ascii.cl/ ) to see what each one is. You will notice that the first 32 are reserved for computer related functions. The English language needs 7 binary digits (bits) to represent all the ones that we need (127 total). An eight bit was added for historical reasons (as a parity bit to make sure that no ones were lost in the transmission). That is why a computer word is 8 bits. Text files use this mapping to save the text. Binary files remap some of the numbers for other uses (Bold Text, Paragraph markers, etc) and have sections set aside for other functions or data. If you open up a binary file (*.exe, *.doc) in a text editor, you will see some "garbage" characters. That is because they do not map to English characters or are used for other functions. 1. Reading and Writing Files 1. Working with Files - Open/Read/Write/Close LiveCode Commands to work with files:
and the function:
...open up LiveCode, click on the "Dictionary" icon in the top menu bar and look those up. notes 1. Open options
2. Working with Files - Using the URL (Uniform Resource Location) The URL as you know it, refers to the Internet location e.g. "Http://www.google.com" . But it can refer to other resources such as files.
http: a page from a web server Sample Code:
notes: You can then move individual items into fields on the card put URL ( "file:" & myFile ) into field "mydata"
2. Working with Folders and Files 1. Working with Folders - Reading Current Files - User Dialogs The "Answer" command has an extended version - "Answer File" which displays a dialog box to select a file. (Remember, the "answer" command displays a dialog box with buttons.) e.g. answer file "Select a file to open:" if the result is not "Cancel" then put it into x end if or answer file "Select a file to open:" if the result is not "Cancel" then put it into field "data" end if You can limit what files the user has to choose from with the "type" keyword e.g. answer file "Select the text file" with type "Text|txt" 2. Working with Folders - Writing New Files - User Dialogs The "Ask" command has an extended version - "Ask File" which displays a dialog box to enter a filename and location to save a file e.g. ask file "Please name the file:" ask file "Save as" with filter "Text file,*.txt" 3. Practice: 1. Create a simple text editor program with 1 large text field ("data") to open files and edit them. 2. Create a Binary file viewer and use it to look at binary files (make it read only, do NOT save the file or you will render it unusable) 3. Try it with both methods 4. Take one of your games and add a "High Scores" function. At the end of the game, ask the user for their name, then save their name and score to a file (append mode- adds the name/score to the end without overwriting what is already there) |