...

View Full Version : Read to end of text file



tonyp12
01-15-2007, 03:37 AM
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>

TripperTreats
01-15-2007, 03:51 AM
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:


for (var i=0; true; i++) {
var temp = openkeys.readline();
if (temp) keystring[i]=temp else break;
}

shyam
01-15-2007, 03:52 AM
I found this to be a lot simpler


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()
}

tonyp12
01-15-2007, 03:59 AM
shyam, it worked perfect.

shyam
01-15-2007, 04:00 AM
next time peek around here (http://www.devguru.com/technologies/vbscript/quickref/textstream.html)

tonyp12
01-17-2007, 12:01 AM
keys=new Array();

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

david_kw
01-17-2007, 12:32 AM
I imagine he put it there because it was in your code first.

david_kw

tonyp12
01-21-2007, 01:06 AM
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


<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum