Gadi Meyer

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromGadi Meyer
ToMe
Subjecthow 2 read and load a text file?
Date23 September 2004 10:39
Dear Author,
A question I have.
I am trying to build a web page that will take its
data from a text file (located on the server).

I need to read each line and analyze it until EOF.

can tell me how?


Gratefully, 
Gadi Meyer
FromMe
ToGadi Meyer
SubjectRe: how 2 read and load a text file?
Date23 September 2004 11:42
Gadi,

Using a server side programming language like PHP, ASP or Perl ... easy.

Perl - there is probably an easier way - I can't remember:
open(MYFILE,'< filename.txt');
foreach( <MYFILE> ) {
    ... $_ contains the content of that line ...
}
close MYFILE;

PHP - there is probably an easier way - I can't remember:
$myfile = fopen('filename.txt','rb');
$fileContent = fread( $myfile, filesize( 'filename.txt' ) );
fclose($myfile);
$fileArray = preg_split( "/\r?\n/", $fileContent );
foreach( $fileArray as $oneLine ) {
    ... $oneLine contains the content of that line ...
}

ASP:
I do not know this language so I cannot give you an example


Using JavaScript, quite simply, you cannot.

JavaScript does not have any way to import text files.

It would be possible to load the text file into an iframe, and attempt to
read the content from there using:
window.frames['iframe name'].document.body.childNodes[0].nodeValue;
but this will only work in Opera and Mozilla (and they will split the text
into muliple child nodes). Internet explorer could probably use ActiveX,
but this would only work within file:// URLs, not http:// (for security
reasons)

It would be better if you could convert the text file into a javascript
variable or XML, and then import that, since both of these are possible:
http://www.howtocreate.co.uk/loadingExternalData.html
http://www.howtocreate.co.uk/tutorials/jsexamples/importingXML.html


Hope you can find something useful in that.

Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromGadi Meyer
ToMe
SubjectRe: how 2 read and load a text file?
Date23 September 2004 21:38
Thank you much
_Gadi
FromGadi Meyer
ToMe
SubjectJS or VBscript?
Date27 September 2004 11:49
I have decided to load my data from XML file.
question?
what is preferable?
BBscript or JavaScript ?

Thanks again
_Gadi
FromMe
ToGadi Meyer
SubjectRe: JS or VBscript?
Date27 September 2004 13:58
Always, always use JavaScript.

VBscript only works in Internet Explorer, and in fact makes it quite hard
to do even basic XML manipulations.

JavaScript good. VBscript bad.
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.