Reading a (BIG) text file one line at a time

Gordon Tillman got at mindspring.com
Mon Nov 22 15:14:08 EST 2004


Hi Rob,

On Nov 22, 2004, at 14:05, Rob Beynon wrote:

> Greetings all,
> I have failed to discover how to read a file one line at a time. The
> file is a text file, and is large (84MB) but I need to process it on a
> line by line basis, rather than read the whole file into a field (not
> even sure I can!).
>
> I thought of
>
> read from myFile until EOF for one line
>
> but that doesn't work
>
> Help would be greatly appreciated here! Would you be good enough to
> mail to my work address, as I have yet to work out how to access the
> list effectively

Once you open your file you can use:

read from file "your_file" for 1 line

The line that is read is put into the "it" variable
When you hit the end of the file then "the result" will no longer be 
empty.  It will have "eof" in it.

For example, assuming the file tFile is open:

repeat forever
     read from file tFile for 1 line
     put it into tLine
     put the result into tRes

     if tRes is not empty then exit repeat

     -- do something with tLine
end repeat

close file tFile


--gordon



More information about the use-livecode mailing list