7 Replies Latest reply on May 15, 2008 9:03 AM by Newsgroup_User

    Read/Write mode in FileIO doesn't work?

    Level 7
      It seems it's not possible to both read from and write to a file in the same
      openFile session? I had the following code:

      fObj=xtra("FileIO").new()
      fObj.openFile(the moviePath & fName,0)
      fObj.writeString(fileString)
      fObj.closeFile()
      fObj=0

      fName and fileString are preset before this starts, but are not relevant to
      the issue. This code was working for saving existing files, but wouldn't
      create files if they didn't exist already. So I added the following:

      fObj=xtra("FileIO").new()
      fObj.openFile(the moviePath & fName,0)
      if fObj.readFile() = VOID then
      fObj.createFile(the moviePath & fName)
      fObj.openFile(the moviePath & fName,0)
      end if
      fObj.writeString(fileString)
      fObj.closeFile()
      fObj=0

      Basically, I'm checking and see if the file exists already or not. It opens
      it and reads it to see if there's anything there already. If not, it
      creates the file and opens it. This did the opposite, working fine on new
      files but wouldn't save data to existing files. So I had to add yet another
      chunk of code to make it work all around:

      fObj=xtra("FileIO").new()
      fObj.openFile(the moviePath & fName,0)
      if fObj.readFile() = VOID then
      fObj.createFile(the moviePath & fName)
      fObj.openFile(the moviePath & fName,0)
      else
      fObj.closeFile()
      fObj.openFile(the moviePath & fName,0)
      end if
      fObj.writeString(fileString)
      fObj.closeFile()
      fObj=0

      So if the file does exist, it has to close it and then open it again in
      order to write to it? I thought that mode 0 was supposed to be
      "Read/Write". So I should be able to both, but apparently not in the same
      session? Is there a better way to check if the file exists first? Seems
      like I'm using twice as much code as should be necessary to do this...


        • 1. Re: Read/Write mode in FileIO doesn't work?
          Level 7
          "Darrel Hoffman" <fake@address.com> posted in macromedia.director.lingo:


          > So if the file does exist, it has to close it and then open it again in
          > order to write to it?

          No, but strange as it seems, it does have to open it before it can delete it.

          > I thought that mode 0 was supposed to be
          > "Read/Write". So I should be able to both, but apparently not in the
          > same session? Is there a better way to check if the file exists first?
          > Seems like I'm using twice as much code as should be necessary to do
          > this...

          Buddy API*, of course. :)
          baFileExists(), baDeleteFile().

          Is there any chance that you still have the file open? This can happen during
          authoring if the script stops for any reason before issuing closeFile().

          But the real trick with fileIO is using both status() and error() (if status
          returns other than 0) after nearly every operation.

          IIRC, the process for deleting a file without a 3rd party Xtra goes something
          like:

          destroy object if exists
          new()
          openFile()
          deleteFile()
          createFile()
          writeFile()
          closeFile()
          destroy object

          Also, if I recall correctly, there is no harm in calling deleteFile() when
          the file does not already exist.

          Here's a DOUG article that might help:
          "Text Files and the FileIO Xtra"
          http://director-online.com/buildArticle.php?id=896


          * Or any of a number of Xtras that include similar features, DirectOS,
          FileXtra, etc...


          --
          Mark A. Boyd
          Keep-On-Learnin' :)
          • 2. Re: Read/Write mode in FileIO doesn't work?
            stephen_ Level 1
            The readFile you were using to check the file status would set the position to the end of file so any writeString would be added to the end of the existing file.

            • 3. Re: Read/Write mode in FileIO doesn't work?
              Level 7
              > The readFile you were using to check the file status would set the
              > position to the end of file so any writeString would be added to the end
              > of the existing file.

              Ah - that sounds about right - checking now and seeing that my test file is
              10x larger than it should be from all my failed saves, and yes - lots of
              repeated data. It doesn't interfere with the reading of the file as it's
              only checking for the first N sections, and not noticing that there are Nx9
              more sections afterwards. So adding a setPosition(0) should fix this?
              Let's see...


              • 4. Re: Read/Write mode in FileIO doesn't work?
                Level 7
                > No, but strange as it seems, it does have to open it before it can delete
                > it.

                Not deleting anything just yet, only creating new or altering old.

                > Buddy API*, of course. :)
                > baFileExists(), baDeleteFile().

                Was afraid of that, I generally try to avoid adding Xtras to a project
                unless there's really no other way. I'm sure at some point Buddy API may be
                added to this project's Xtra list, but until I need to, I'm holding back a
                bit.

                > Is there any chance that you still have the file open? This can happen
                > during
                > authoring if the script stops for any reason before issuing closeFile().

                I'm almost certain that's not the case. I make a pretty strict habit of
                closing files as soon as possible after opening them - open-read-close or
                open-write-close. Since the program hasn't actually errored at all during
                this process, I can safely assume that the closeFile() is happening as it
                should.

                > But the real trick with fileIO is using both status() and error() (if
                > status
                > returns other than 0) after nearly every operation.

                I was trying to see what those did, but status() returned 0 every time, even
                when it didn't do what it was supposed to. Error() I didn't check, but
                without a code to put into it, it doesn't seem like it'd do anything. The
                other one I was looking at was getLength() - presumably that'd return 0 for
                a non-existent file?

                > IIRC, the process for deleting a file without a 3rd party Xtra goes
                > something
                > like:

                Well, as I said I'm not deleting anything, so I don't think I need that yet.
                I think Stephen hit the nail though - it actually was writing, just in the
                wrong place.


                • 5. Re: Read/Write mode in FileIO doesn't work?
                  Level 7
                  >> But the real trick with fileIO is using both status() and error() (if
                  >> status
                  >> returns other than 0) after nearly every operation.
                  >
                  > I was trying to see what those did, but status() returned 0 every time,
                  > even when it didn't do what it was supposed to. Error() I didn't check,
                  > but without a code to put into it, it doesn't seem like it'd do anything.
                  > The other one I was looking at was getLength() - presumably that'd return
                  > 0 for a non-existent file?

                  Ugh, I figured it out. Status() wasn't returning 0 but -37. I was doing
                  "if fObj.status() > 0 then" etc. I forgot that error messages come in as
                  negative numbers. D'oh.


                  • 6. Re: Read/Write mode in FileIO doesn't work?
                    Level 7
                    >> Buddy API*, of course. :)
                    >> baFileExists(), baDeleteFile().
                    >
                    > Was afraid of that, I generally try to avoid adding Xtras to a project
                    > unless there's really no other way. I'm sure at some point Buddy API may be
                    > added to this project's Xtra list, but until I need to, I'm holding back a
                    > bit.

                    I'm a little confused by this. FileIO is an xtra too, and you're adding
                    it...

                    You've found a solution already, but FWIW PRegEx has single line
                    read/write functions, is blazingly fast, free, and allows regular
                    expression searches/matches/replaces among other useful functions
                    • 7. Re: Read/Write mode in FileIO doesn't work?
                      Level 7
                      >> Was afraid of that, I generally try to avoid adding Xtras to a project
                      >> unless there's really no other way. I'm sure at some point Buddy API may
                      >> be added to this project's Xtra list, but until I need to, I'm holding
                      >> back a bit.
                      >
                      > I'm a little confused by this. FileIO is an xtra too, and you're adding
                      > it...

                      Key word "until I need to". I couldn't do ANY of this without FileIO, so
                      obviously I need that. Also, I go back and forth between multiple machines
                      with this project (home machine, work machine, a laptop for in between, and
                      sending the files to be viewed remotely by other people on the project,
                      etc.), and I don't want to have to worry about making sure that I have all
                      the Xtras on every machine. FileIO comes with Director, so it's guaranteed
                      to be there no matter where I take it.

                      > You've found a solution already, but FWIW PRegEx has single line
                      > read/write functions, is blazingly fast, free, and allows regular
                      > expression searches/matches/replaces among other useful functions

                      Hmm, I've never heard of that one, sounds interesting but changing the
                      pipeline this late into the project would be somewhat prohibitive in terms
                      of complexity. Another reason I don't like adding new Xtras. If I were
                      using it from the beginning, maybe it'd be one thing, but as big as this
                      thing is right now, I don't want to throw a wrench in the works by
                      fundamentally changing half my code like that...