Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-15-2007, 02:37 AM   PM User | #1
tonyp12
New Coder

 
Join Date: Jan 2007
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
tonyp12 has a little shameless behaviour in the past
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..
tonyp12 is offline   Reply With Quote
Old 01-15-2007, 02:51 AM   PM User | #2
TripperTreats
New Coder

 
TripperTreats's Avatar
 
Join Date: Oct 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
TripperTreats is an unknown quantity at this point
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
."
TripperTreats is offline   Reply With Quote
Old 01-15-2007, 02:52 AM   PM User | #3
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
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()
}
shyam is offline   Reply With Quote
Old 01-15-2007, 02:59 AM   PM User | #4
tonyp12
New Coder

 
Join Date: Jan 2007
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
tonyp12 has a little shameless behaviour in the past
shyam, it worked perfect.
tonyp12 is offline   Reply With Quote
Old 01-15-2007, 03:00 AM   PM User | #5
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
next time peek around here
shyam is offline   Reply With Quote
Old 01-16-2007, 11:01 PM   PM User | #6
tonyp12
New Coder

 
Join Date: Jan 2007
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
tonyp12 has a little shameless behaviour in the past
keys=new Array();

Why is this there?, I can not see that it used.
tonyp12 is offline   Reply With Quote
Old 01-16-2007, 11:32 PM   PM User | #7
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
I imagine he put it there because it was in your code first.

david_kw
david_kw is offline   Reply With Quote
Old 01-21-2007, 12:06 AM   PM User | #8
tonyp12
New Coder

 
Join Date: Jan 2007
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
tonyp12 has a little shameless behaviour in the past
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..
tonyp12 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:36 AM.


Advertisement
Log in to turn off these ads.