Files

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:

Open file
Read from file
Write to file
Close file

and the function: 
specialFolderPath

...open up LiveCode, click on the "Dictionary" icon in the top menu bar and look those up.

    notes

            1. Open options
    open file  "scores.txt"        - opens a file
    open file  "scores.txt"  for read      - opens a file only for reading
    open file  "scores.txt"  for write      - opens a file for writing
    open file  "scores.txt"  for append  - opens a file and adds to the end
    open file  "scores.txt"  for read      - opens a file only for reading
:
  2. specialFolderPath - Since each platform (Windows/Mac/Linux/iOS/Android/etc uses different folders or directories, it is hard to keep track of where files are saved. LiveCode keeps track of that for you and simplifies it with this function

    e.g.     User documents are automatically stored in different places on each platform:
                 - Windows - in "C:/Documents and Settings/Users/Smith/My Documents"
                 - Mac - "/Users/Smith/Documents"
                 - Linux - "/home/Smith"

        So you need code to save the files in different places:
            if System = Windows then
                    filePath = "C:/Documents and Settings/Users/Smith/My Documents" 
            if System = "Mac" then
                    ...
   
         In LiveCode all you need to do is say:
                specialFolderPath("Documents") and it will be saved in the correct place no matter which system you are running on

    3. When you open up a file, you can specify the type (text/binary) and the mode (read/write/append)

Sample Code:

    A. Reading a file into a text field called "highScores" on the card

on mouseUp 
    put specialFolderPath("Documents") & "/Scores.txt" into myFile
    open file myFile for read 
    read from file myFile until EOF 
    put it into fld "highScores" 
    close file myFile 
end mouseUp

    B. Reading a file into a variable called "x"

on mouseUp 
    put specialFolderPath("Documents") & "/Scores.txt" into myFile
    open file myFile for read 
    read from file myFile until EOF 
    put it into x 
    close file myFile
end mouseUp


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
        ftp: a directory or file on an FTP server
        file: a text file on the local disk (not on a server)
        binfile: a binary file

    Sample Code:


    A. Reading a file into a text field called "myScores" on the card

on mouseUp 
    put specialFolderPath("Documents") & "/Scores.txt" into myFile
    put URL ( "file:" & myFile ) into field "myScores" 
end mouseUp

    B. Reading a file into a variable called "x"

on mouseUp 
    put specialFolderPath("Documents") & "/Scores.txt" into myFile
    put URL ( "file:" & myFile ) into x 
end mouseUp

        notes:

            You can then move individual items into fields on the card

                   put URL ( "file:" & myFile ) into field "mydata" 

       if it is not empty then
           put item 1 of line 1  of field "mydata" into name
           put item 1 of line 2  of field "mydata" into  highScore
           put item 1 of line 3  of field "mydata" into  theDate
           put item 1 of line 4  of field "mydata" into  theTime
        end if

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)
Comments