4 Replies Latest reply on Mar 26, 2007 4:05 AM by Newsgroup_User

    A fileio problem

    wgb14 Level 1
      Hi all,

      I have the following code:

      global customscript,whichAIML

      -- write the aiml file

      on saveAnINI2
      -- set Question = the text of member "greeklish"
      set Question = the text of member "Q"
      gFileIO =new (xtra "FileIO")
      gFileIO.createFile("aiml\"&whichAIML&"" & ".aiml")
      gFileIO.openFile ("aiml\"&whichAIML&"" & ".aiml",0)
      gFileIO.writeString ("<aiml>" & RETURN)
      setPosition(gFileIO,getLength(gFileIO))
      gFileIO.writeString("<category>" & RETURN & "<pattern>"&Question&"</pattern>" &RETURN & "<template>" & customscript &".hap" & "</template>" & RETURN & "</category>" & RETURN)

      gfileIO = VOID
      end

      Although its working, somehow it writes the same text lines twice. Any ideas what is wrong with it? I want the string to be wrirtten once and not twice. What am i doing wrong?
        • 1. Re: A fileio problem
          johnAq Level 1
          What you might be seeing is a quirk of fileio whereby if you write over an existing file, and the old length is longer than the new length, the extra data from the original is kept at the end of the file.

          To work around this, first delete the old file and create a new empty file in its place and write to that

          hth

          johnAq
          • 2. Re: A fileio problem
            wgb14 Level 1
            Thanks for the reply, the problem is that i want to write the data in the same file. Is there any other solution?
            • 3. Re: A fileio problem
              Level 7
              > the problem is that i want to write the data in the same file.

              Perhaps you could try opening the file before creating it (in case it
              already exists and you want to append to it).
              Or use FileXtra4 or Buddy API to check for its existence before creating it.
              • 4. Re: A fileio problem
                Level 7
                wgb14 wrote:
                > Thanks for the reply, the problem is that i want to write the data in
                > the same file. Is there any other solution?

                1) Read all data in from file.
                2) Modify data in memory.
                3) Save data to a different file.
                4) Delete original file.
                5) Rename saved file to original file.

                Andrew