Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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; 04-03-2009 at 10:09 AM.

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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);
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •