6 Replies Latest reply on Apr 16, 2006 4:16 AM by Newsgroup_User

    Read and write to a file

    jrheeder
      Hi

      I am a lingo novice and I would like to be able to save a string to a .txt. file. I have had a look at the sample applications but when I used the copy of the application and tried to publish it, the script didnt work. So can anyone help me and provide me with a script to read and write to a txt file that acctually works if I publish.

      PLEASE HELP
        • 1. Re: Read and write to a file
          James Newton, ACP Level 3
          Did the sample applications work in Director? If so, did you include all the required Scripting Xtras in your projector?

          You can find a wrapper script for the FileIO xtra and a script that warns you when an xtra that you are trying to use is unavailable at http://nonlinear.openspark.com/tips/scripts/xtras/. You'll need the File Manager and Xtras Broker scripts. Both are Movie Scripts. See the comments at the top of the File Manager script for information on what handlers to call.
          • 2. Re: Read and write to a file
            Level 7
            The fileIO xtra which comes with Director is usually sufficient for
            dealing with external text files, like most other file system xtras it
            won't work in shockwave.

            There's some learning curve incolved in utilizing FileIO


            How to use the FileIO Xtra http://www.macromedia.com/go/tn_14607

            includes downloadable examples



            Text Files and the FileIO Xtra

            http://director-online.com/buildArticle.php?id=896
            • 3. Re: Read and write to a file
              Level 7
              Hi,

              I'll just add my link to the mix:
              http://www.fbe.unsw.edu.au/learning/Director/Lingo/FileIO.asp
              Looks at using the FileIO Xtra to create/edit external text files.

              Wish you all the best with your Director endevours.

              regards
              Dean

              Director Lecturer / Consultant
              http://www.fbe.unsw.edu.au/learning/director
              http://www.multimediacreative.com.au




              • 4. Re: Read and write to a file
                jrheeder Level 1
                I have taken all the advise and set up the following code but it doesnt work at all. Can anyone tell me why not? PLEASE!!!!

                This code is suppose to read from the files and place it on the screen

                global i1,i2,i3

                on exitFrame me
                if objectP(i1) then set i1 = 0 --Delete the instance if it already exists
                i1 = new(xtra "fileio") -- Create an instance of FileIO
                openFile(i1,the moviePath&"name1.txt",1) --Open the file with read access
                myVariable1 = readFile(i1) --set the variable 'myVariable' to the text of the file 'info.txt'
                if readFile(i1)=VOID then --checks to see if this file exists
                alert "File 1 Missing" --alerts if the variable returned VOID
                else
                member("tag box 1").text = myVariable1 -- display the text in a field
                closeFile(i1) -- Close the file
                end if
                i1 = 0 -- Dispose of the instance

                if objectP(i2) then set i2 = 0 --Delete the instance if it already exists
                i2 = new(xtra "fileio") -- Create an instance of FileIO
                openFile(i2,the moviePath&"name2.txt",1) --Open the file with read access
                myVariable2 = readFile(i2) --set the variable 'myVariable' to the text of the file 'info.txt'
                if readFile(i2)=VOID then --checks to see if this file exists
                alert "File 2 Missing" --alerts if the variable returned VOID
                else
                member("tag box 2").text = myVariable2 -- display the text in a field
                closeFile(i2) -- Close the file
                end if
                i2 = 0 -- Dispose of the instance

                if objectP(i3) then set i3 = 0 --Delete the instance if it already exists
                i3 = new(xtra "fileio") -- Create an instance of FileIO
                openFile(i3,the moviePath&"name3.txt",1) --Open the file with read access
                myVariable3 = readFile(i3) --set the variable 'myVariable' to the text of the file 'info.txt'
                if readFile(i3)=VOID then --checks to see if this file exists
                alert "File 3 Missing" --alerts if the variable returned VOID
                else
                member("tag box 3").text = myVariable3 -- display the text in a field
                closeFile(i3) -- Close the file
                end if
                i3 = 0 -- Dispose of the instance
                end



                .... and this is suppose to write to file

                global i1, i2, i3, emptyfile
                on mouseUp me
                emptyfile = FALSE

                if objectP(i1) then set i1 = 0 --Delete the instance if it already exists
                i1 = new(xtra "fileio") -- Create an instance of FileIO
                openFile (i1, the moviePath &"name1.txt",0)--Open the file with R/W access
                myVariable1 = readFile(i1)
                if myVariable1 <> "" then
                delete(i1) --deletes the file
                createFile (i1, the moviePath &"name1.txt") --creates the file again
                openFile(i1,the moviePath &"name1.txt",0) --Open the file with R/W access
                mySaveString = member("New player tag box").text --puts the contents of the field 'input' into the variable mySaveString
                writeString(i1,mySaveString) --writes text to the file
                closeFile(i1) -- Close the file
                i1 = 0 -- Dispose of the instance
                emptyfile = TRUE
                end if

                if objectP(i2) then set i2 = 0 --Delete the instance if it already exists
                i2 = new(xtra "fileio") -- Create an instance of FileIO
                openFile (i2, the moviePath &"name2.txt",0)--Open the file with R/W access
                myVariable2 = readFile(i2)
                if myVariable2 <> "" then
                delete(i2) --deletes the file
                createFile (i2, the moviePath &"name2.txt") --creates the file again
                openFile(i2,the moviePath &"name2.txt",0) --Open the file with R/W access
                mySaveString = member("New player tag box").text --puts the contents of the field 'input' into the variable mySaveString
                writeString(i2,mySaveString) --writes text to the file
                closeFile(i2) -- Close the file
                i2 = 0 -- Dispose of the instance
                emptyfile = TRUE
                end if

                if objectP(i3) then set i3 = 0 --Delete the instance if it already exists
                i3 = new(xtra "fileio") -- Create an instance of FileIO
                openFile (i3, the moviePath &"name3.txt",0)--Open the file with R/W access
                myVariable3 = readFile(i3)
                if myVariable3 <> "" then
                delete(i3) --deletes the file
                createFile (i3, the moviePath &"name3.txt") --creates the file again
                openFile(i3,the moviePath &"name3.txt",0) --Open the file with R/W access
                mySaveString = member("New player tag box").text --puts the contents of the field 'input' into the variable mySaveString
                writeString(i3,mySaveString) --writes text to the file
                closeFile(i3) -- Close the file
                i3 = 0 -- Dispose of the instance
                emptyfile = TRUE
                end if

                if emptyfile = FALSE then
                if objectP(i1) then set i1 = 0 --Delete the instance if it already exists
                if objectP(i2) then set i2 = 0
                if objectP(i3) then set i3 = 0
                i1 = new(xtra "fileio") -- Create an instance of FileIO
                i2 = new(xtra "fileio")
                i3 = new(xtra "fileio")
                openFile (i1, the moviePath &"name1.txt",0)--Open the file with R/W access
                openFile (i2, the moviePath &"name2.txt",0)
                openFile (i3, the moviePath &"name3.txt",0)
                myVariable1 = readFile(i1)
                myVariable2 = readFile(i2)
                myVariable3 = readFile(i3)
                delete(i1) --deletes the file
                delete(i2)
                delete(i3)
                createFile (i1, the moviePath &"name1.txt") --creates the file again
                createFile (i2, the moviePath &"name2.txt")
                createFile (i2, the moviePath &"name3.txt")
                mySaveString = member("New player tag box").text --puts the contents of the field 'input' into the variable mySaveString
                writeString(i1,mySaveString) --writes text to the file
                writeString(i2,myVariable1)
                writeString(i3,myVariable2)
                closeFile(i1) -- Close the file
                closeFile(i2)
                closeFile(i3)
                i1 = 0 -- Dispose of the instance
                i2 = 0
                i3 = 0
                emptyfile = TRUE
                end if
                go to frame 514
                end


                I also tried to do something different. If all three files are full then File 3 gets cleared and file 2 becomes file 3 and file 1 becomes file 2 and the new data gets save in file 1 ie. a queue concept. F.I.F.O (first in first out)
                • 5. Re: Read and write to a file
                  James Newton, ACP Level 3
                  See my answer to your earlier question on the Director Online forums.
                  • 6. Re: Read and write to a file
                    Level 7
                    Hi,

                    I think James' reply above was emant to be in reply to this post. Just in case you
                    missed it, he suuggested you look at his response at:
                    http://director-online.com/forums/read.php?2,16305

                    regards
                    Dean

                    Director Lecturer / Consultant
                    http://www.fbe.unsw.edu.au/learning/director
                    http://www.multimediacreative.com.au