Web Analytics Made Easy -
StatCounter Reading past end of file error - CodingForum

Announcement

Collapse
No announcement yet.

Reading past end of file error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading past end of file error

    Hi

    I'm reading a text file, manipulating the string that I've read, and then writing to a new text file called temp. The error that I am getting is basically reading past the end of the file.

    function SpaceFileEntries()
    {
    fso = new ActiveXObject("Scripting.FileSystemObject");

    filename = "NDELPROF.txt";
    path1 = document.getElementById("lblDataOutputDirectory").innerText + "\\" + filename;
    path2 = document.getElementById("lblDataOutputDirectory").innerText + "\\temp.txt";
    s = fso.OpenTextFile(path1);
    j = fso.CreateTextFile(path2 , true);


    do
    {

    text = s.readLine();
    partOne = text.substring(0,34);
    lastPart = text.substring(35, 54);
    space = " ";
    j.write(partOne);
    j.write(space);
    j.writeline(lastPart);

    } while ( text != null )


    s.Close();
    j.Close();

    }

    Can someone please help me find this bug in the code. I know it's got to do with the while statement, but can't think or find good alternatives.

    Thanks.
    Daniel

  • #2
    The problem is your do...while loop.

    while ( text != null ) is evaluated for a boolean value of true or false. If the evaluation returns true the loop starts again at the do keyword, but on the final loop text = s.readLine(); reads past the end of the file.

    Use a simple while loop and then:

    text = s.readLine();
    if (text) { // continue if text has a value

    There is no way to detect the end of file EOF in Javascript. Perhaps you ought to use VBScript instead.



    He thought he saw a Banker's Clerk
    Descending from the bus:
    He looked again, and found it was
    A Hippopotamus.
    "If this should stay to dine," he said,
    "There won't be much for us!"
    -Lewis Carroll
    Last edited by Philip M; Apr 3, 2009, 04:09 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      I must differ with your opinion that "There is no way to detect the end of file EOF in Javascript"

      Try this function which uses the EndOfStream property


      function GetALine(filespec)
      {
      var fso, f, s, ForReading;
      ForReading = 1, s = "";
      fso = new ActiveXObject("Scripting.FileSystemObject");
      f = fso.OpenTextFile(filespec, ForReading, false);
      while (!f.AtEndOfStream)
      s += f.ReadLine( );
      f.Close( );
      return(s);
      }

      Comment

      Working...
      X