4 Replies Latest reply on Dec 1, 2011 1:04 PM by TᴀW

    File I/O bizareness

    TᴀW Adobe Community Professional & MVP

      Hi,

       

      Please could someone explain this to me. Here's a very simple script:

       

      var f = File.openDialog();

      f.open("r");

      alert(f.readln());

      f.close();

      f.open("r");

      alert(f.readln());

      f.close()

       

      In some strange cases, the two alerts are not the same!

      If I select a plain text file, then if say the first line of text is

      "Title" then the first alert produces "Title" and the next alert

      produces "ETitle" or "3Title".

       

      How on earth can that be?!

       

      I will note that right now this is only happening on a text file which

      was created on a Mac and which I'm now opening in Windows. [The way the

      file was created on Mac was through InDesign scripting -- very simple --

      the user get a save as dialog, and then the script does open("w"),

      several writeln(), and then a close().]

       

      I'd be really grateful if someone could tell me what's going on here.

       

      Thank you,

      Ariel

       

        • 1. Re: File I/O bizareness
          Harbs. Level 6

          It's probably related to encoding issues.

           

          Before reading the file try this:

          myFile.encoding = "UTF8";

           

          Harbs

          • 2. Re: File I/O bizareness
            TᴀW Adobe Community Professional & MVP

            Thanks Harbs.

             

            Didn't help. Is this after the file has been opened (open("r")) of before?

             

            What has helped is after the file is opened I do a File.seek(0) -- which

            seems to set the counter back. But I don't understand why the alerts are

            DIFFERENT each time.

             

            Thanks,

            Ariel

            • 3. Re: File I/O bizareness
              Harbs. Level 6

              I tend to avoid using readln() I tend to use read() and work with the whole file string directly.

               

              I've had trouble getting readln() to work as I'd expect. Instead of struggling with it, I just took the easy way out and avoided it altogether...

               

              Harbs

              • 4. Re: File I/O bizareness
                TᴀW Adobe Community Professional & MVP

                Readln() seems to work for me, except for this weird thing. I had a look

                at the txt file in a hex editor, and there's nothing there except for

                the text and the line break characters (CR/LF on Windows and CR only on

                Mac).

                 

                In other words, there's no encoding bytes at all either on Windows or Mac.

                 

                And seek(0) seems to get things working again.

                 

                Still, I'd be curious to know what's going on. I would have thought that

                a File.close() followed by a File.open("r") would reset any internal

                counters.

                 

                Ariel