Re: Read file bug?
Re: Read file bug?
- Subject: Re: Read file bug?
- From: John Delacour <email@hidden>
- Date: Thu, 17 Apr 2003 10:58:57 +0100
- Mac-eudora-version: 6.0a15
At 2:13 pm +1000 17/4/03, Timothy Bates wrote:
If I open a file and read until the end of a line, I expect to eventually
error as we hit the eof.
But not if you *never find* an end of line! You were trying to read
a file delimited with carriage returns and not line feeds, so the
script was reading until doomsday on the first iteration.
This code, however, loops forever. Bug or misunderstanding. How does one
know that the last line has been read?
From the error, as you supposed. So you must add a try block to exit
at eof. The last line will be read nevertheless because eof,
contrary to popular belief, is not a character in the last line.
Here's a reliable way to do the thing, and I'll post a neater way in
a follow-up message.
(* E & OE *)
set fU to "/Users/jd/Documents/Eudora
Folder/eunotice_folder/READ_ME.rtfd/TXT.rtf"
set f to POSIX file fU
set linefeed_ to ASCII character 10
set separator_ to linefeed_
(* Always start with this . You will need to swear less *)
repeat
try
close access f
on error
exit repeat
end try
end repeat
(* Determine what paragraph separator_ is used *)
open for access f
try
(* NB chunk_ will always be got, even if you reach eof *)
set chunk_ to read f for 1000 -- or a number as big as you like
set shift_ to false
if flag_ to chunk_ contains return then
if flag_ and chunk_ contains linefeed_ then
set separator_ to return & linefeed_
set shift_ to true
end if
end if
on error e
end try
close access f
(* Read line by line and print lines containing pattern_ *)
set pattern_ to "file"
set n to 0
open for access f
repeat
set n to n + 1
try
set line_ to read f until separator_
(* Remove leading linefeeds from PC files *)
if shift_ then set line_ to text 2 through -1 of line_
on error e
log e & ".. but I did read the last line! "
exit repeat
end try
(* Print the line if it contains the pattern *)
if line_ contains pattern_ then log "(" & n & ")" & tab & line_
end repeat
close access f
-- JD
property pPathToDataFile : "path:to:a:text:file"
set f to open for access file pPathToDataFile
set targetString to "not in this file"
set n to 1
set nextN to 1
repeat
set a to read f until ASCII character 10
if n > nextN then
postit n & "" & a as string
set nextN to n + 50
end if
set n to n + 1
end repeat
close access file pPathToDataFile
_______________________________________________
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.