Re: Text file reading... (David Crowe)
Re: Text file reading... (David Crowe)
- Subject: Re: Text file reading... (David Crowe)
- From: Axel Luttgens <email@hidden>
- Date: Tue, 07 Jan 2003 09:48:52 +0100
David Crowe wrote:
>
Thanks to Paul Berkowitz and Gary Lists for their assistance
>
regarding reading a text file that might be delimited by either
>
linefeeds or carriage returns.
>
>
>
Paul suggested reading the whole file and then reading out paragraphs,
>
which don't care about the difference between CR and LF. That leaves
>
the problem, however, of when the file gets too big (I presume 4060
>
bytes is the largest).
Not exactly: the file could be very large but, by using Paul's second
suggestion ('set theLines to paragraphs of r'), you could end with a
list of strings counting more than about 4060 items. And there's a bug
in AppleScript when it is asked to produce such lengthy lists that way.
>
>
>
Gary suggested reading "to eof". I presume that this is the same
>
thing, to read the entire file in one swoop, and then parse out the
>
lines later.
Note that Paul's approach relied on reading the file in one swoop
(through statement 'set r to read theFile') too.
>
>
>
It seems like this is the best solution. If size gets to be a problem
>
(e.g. files larger than 4k),
With the above: a file counting more than (about) 4060 lines.
>
I can always read the file in chunks and pull lines out of the chunk
>
as necessary, taking care to handle lines that cross chunk boundaries.
>
>
- David Crowe
Your initial post as well as this one tend to indicate you would prefer
a "read line by line" approach.
It is often the case that there is some upper-bound for the length of
the lines of a line-oriented file.
If this is your case, and if your files have either CR or LF-ended
lines, but not both, and if neither CR nor LF may appear in regular
data, you could try following approach (pseudo-code):
read a chunk of maximum expected bytes for a line (line-ending
character included)
if that chunk contains CR then
this file uses CR as line delimiter
else if that chunk contains LF then
this file uses LF as line delimiter
else if the read chunk is has same length as the file
the file is a one-liner
else
the upper-bound was badly choosen
HTH,
Axel
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.