5 Replies Latest reply on Feb 7, 2007 5:29 PM by Lukewig

    Writing (appending) to  a txt file

    comedydave
      Hi,

      I have the code that is the example used on the adobe site for adding text to an existing txt file but i've tried and tried and it doesn't work!

      Just wondering if i have the right code or if anyone has some code that does work!

      Thanks,

      Dave
        • 1. Re: Writing (appending) to  a txt file
          Level 7
          Define "doesn't work" :-)
          What does the alert tell you about the writeString() status?
          • 2. Re: Writing (appending) to  a txt file
            comedydave Level 1
            I get no error messages, all seems to work fine until you check the text file which still contains nothing!
            • 3. Re: Writing (appending) to  a txt file
              Level 7
              Assuming you have a *field* named "myField" (adjust type and name as
              appropriate), try the following in your authoring environment - there
              should be messages in your message window that will help with debugging
              --
              global myFile

              on mouseUp me
              if objectP(myFile) then myFile = 0
              tText = the text of field "myfield"
              myFile = new(xtra "fileio")
              openFile(myfile, the moviepath & "test.txt", 0)
              put "openFile():", status(myFile)
              setPosition(myfile, getLength(myFile))
              put "setPosition():", status(myFile)
              writeString(myFile, tText)
              put "writeString():", status(myFile)
              alert "Status: "& error(myFile, status(myFile))
              closeFile(myfile)
              myFile = 0
              end
              • 4. Re: Writing (appending) to  a txt file
                comedydave Level 1
                Doh!

                I see where i've gone wrong, i had flash components rather than standard!

                I always do that and it always confuses me!

                Thanks alot, all working now!

                Dave
                • 5. Re: Writing (appending) to  a txt file
                  Lukewig
                  Hi Dave,

                  Just a quick note (or two)

                  1. I'm not sure why you're instantiating Fileio with each exitframe (I wouldn't - you're just using up resources)

                  2. myFile doesn't need to a global (since you are instantiating and destroying it with each mouseUp - just use a local variable. Let chance of leaks).

                  3. Consider using a wrapper function (attached below) which you would use like this (and reuse in other projects, or change to use setPref or vList if you later decided to port your project to SHockwave, for example)

                  on mouseUp
                  res = AppendText("test.txt", member("myfield").text)
                  put "Appended text result: " & res
                  end