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 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Read to end of text file

    This programs works to read a file, but only if I know how many lines of text there are.

    How do I tell the loop to stop when end of file?

    hint: save this code with .hta extension instead of .html (no warnings)

    <html>
    <head>
    <title></title>
    <script>

    function Readfile()
    {
    keys=new Array();
    var fso=new ActiveXObject("Scripting.FileSystemObject");
    var openkeys=fso.openTextFile("C:\\testfile.txt",1);
    var keystring=new Array();

    i = 0
    while (i <= 10) // this wile stop after 10 read lines, (keystring[i]!= null) does not work
    {
    keystring[i]=openkeys.readline();
    i++
    }

    for(var i=0;i<10;i++){
    document.write(keystring[i] +"<br>")
    }

    openkeys.close()
    }
    </script>
    </head>
    <body>
    <a href="#" onClick="Readfile()">Click to read txt file</a>
    </body>
    </html>
    Last edited by tonyp12; 01-15-2007 at 02:51 AM.

  • #2
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know anything about using ActiveXObject, but I'm assuming that if you try to readline() at the end of the document, it will return null or false or something. So can you not do something like this:

    Code:
    for (var i=0; true; i++) {
      var temp = openkeys.readline();
      if (temp) keystring[i]=temp else break;
    }
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #3
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    I found this to be a lot simpler

    Code:
    function Readfile() {
      keys=new Array();
      var fso=new ActiveXObject("Scripting.FileSystemObject");
      var openkeys=fso.openTextFile("C:\\testfile.txt",1);
      var keystring=[];
      var buf = openkeys.readall();
      keystring = buf.split('\n'); // to get all individual lines
      document.write(keystring.join('<br>'));
      openkeys.close()
    }

  • #4
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    shyam, it worked perfect.

  • #5
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    next time peek around here

  • #6
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    keys=new Array();

    Why is this there?, I can not see that it used.

  • #7
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I imagine he put it there because it was in your code first.

    david_kw

  • #8
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I also found out how to read unicode, the -1 is for unicode

    var openkeys=fso.openTextFile("testtext.txt",1,false,-1);

    If I use charCodeAt(0) for a file that starts with hex 41 42 (text for AB)
    I get an answer of 16961 = 42 41 (text for BA)
    So it kind of pairs them in reverse, so some bit shifting have to be done of if I want to get the bytes.

    But if you want to read pure raw data you have to use read(number of bytes/2)
    instead of readall()

    Example that reads the notepad.exe file in rawdata mode and
    shows the hex value of 16 bytes starting on offset 132.
    Rename to .hta

    Code:
    <html>
    <head>
    <title>Show hex value of data in file</title>
    
    <script>
    
     function Readfile() {
      
      var fso=new ActiveXObject("Scripting.FileSystemObject");
      var openkeys=fso.openTextFile("C:\\WINDOWS/NOTEPAD.EXE",1,false,-1);
      var buf = openkeys.read(66);     // read first 132 bytes
      var buf = openkeys.read(8);      // read the 16 bytes we want.
    
      var hD='0123456789ABCDEF';
      for (var i=0; i<8; i++)
      {
      var d= buf.charCodeAt(i) & 255;   // get the fist 8 bits
      if (d<16){document.write('0')};
      var h = hD.substr(d&15,1);
      while (d>15) {
      d>>=4;
      h=hD.substr(d&15,1)+h;
       }  
      document.write(h + ' ');
    
      var d= buf.charCodeAt(i)>>8;       // get the top 8 bits
      if (d<16){document.write('0')};
      var h = hD.substr(d&15,1);
      while (d>15) {
      d>>=4;
      h=hD.substr(d&15,1)+h;
       }  
      document.write(h + ' ');
      }
    
      openkeys.close()
    }
    
    
    </script>
    </head>
    <body onLoad="Readfile()">
    
    </body>
    </html>
    Last edited by tonyp12; 01-21-2007 at 09:08 PM.


  •  

    Posting Permissions

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