reading from a file

John Ridge ridge11103 at btinternet.com
Sun Jun 12 13:06:52 EDT 2005


on 12/6/05 1:21 pm,  Alex Tweedly wrote :

John Ridge wrote:

>I need to read from a text file until I hit EOF - but in the meantime I want
>to process each of the chunks I'm reading (separated by delimiters).
>
>It seemed obvious and natural to start off, after opening the file,
>   
>       repeat until EOF
>       read from file xxx until <delimiter>
> ...process next chunk...
>       end repeat
>
>But, alas, this runs forever - it doesn't encounter EOF, although I was
>careful to "close" the file when I wrote it out. I can't find anything in
>the docs that explains why it doesn't work. Should it?
>
>Apologies if the answer is just too obvious!
>John
>  
>
EOF is simply a constant for "an end of file character"

One way you can do what you want  (there are bound to be others :-) is ...

  open file tName for read
   repeat forever
      read from file tName until cr
      if the result = "eof" then exit repeat
      put it & cr after msg    -- to check it is being read
 end repeat
 put "done" & cr after msg    -- to check we exit OK

... note "result" can be set to other values if there is an error -
depending on circumstances, you may want to check for that happening.

***************************
Thanks, Alex - of course it's "the result" that I want - but I still like my
syntax! What language am I half-remembering?

I've tried another method that I thought might be faster - instead of
reading a chunk at a time and popping it into a field on a new card, why not
gulp the whole file into a variable, then get each item as a chunk?

In fact it's slower. Ah well...
-- 




More information about the use-livecode mailing list