1. Looping thru file with 'gets()' fails to retrieve last record
- Posted by John F Dutcher <John_Dutcher at urmc.rochester.edu> Aug 05, 2004
- 444 views
I have an Ascii text file in the cgi-bin with exactly 3,772 records in it. I have tried the two approaches below to read it.....both fail to return the last record before posting a '-1' to the file handle. Both return exactly 3,771 records instead of 3,772 ?? First, the file is opened and one record read:
integer fn sequence line fn=open("bcare_remit.txt","r") if fn > 0 then line = gets(fn) <----- 1st record retrieved here else puts(1,"Unable to open the disk file\n") abort(1) end if
The above is followed by either of the two loops below: #1:
while sequence(line) do ......process... line = gets(fn) end while
#2:
for 1 = 1 to 3772 do ......process... line = gets(fn) end for
2. Re: Looping thru file with 'gets()' fails to retrieve last record
- Posted by Derek Parnell <ddparnell at bigpond.com> Aug 05, 2004
- 418 views
John F Dutcher wrote: > > I have an Ascii text file in the cgi-bin with exactly 3,772 records in it. > > I have tried the two approaches below to read it.....both fail to return > the last record before posting a '-1' to the file handle. Both return > exactly 3,771 records instead of 3,772 ?? > integer fn > sequence line > fn=open("bcare_remit.txt","r") > if fn > 0 then > line = gets(fn) <----- 1st record retrieved here > else > puts(1,"Unable to open the disk file\n") > abort(1) > end if The variable 'line' should really be an object rather than a sequence. This is because the gets() routine will return an atom after the last line in read. What does the following code produce on your system? integer fn object line integer cnt fn=open("bcare_remit.txt","r") if fn != -1 then cnt = 0 line = gets(fn) while sequence(line) do cnt += 1 line = gets(fn) end while ? cnt end if Also, ensure that there is no embedded Ctrl-Z (ascii-26) in your file. -- Derek Parnell Melbourne, Australia
3. Re: Looping thru file with 'gets()' fails to retrieve last record
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Aug 05, 2004
- 450 views
On Wed, 04 Aug 2004 17:32:19 -0700, John F Dutcher <guest at RapidEuphoria.com> wrote: <snip> > line = gets(fn) <----- 1st record retrieved here <snip> >for 1 = 1 to 3772 do > ......process... > line = gets(fn) >end for That is 3773 calls to gets(). Make line an object, which will allow the while loop to work. Pete
4. Re: Looping thru file with 'gets()' fails to retrieve last record
- Posted by irv mullins <irvm at ellijay.com> Aug 05, 2004
- 429 views
John F Dutcher wrote: > > I have an Ascii text file in the cgi-bin with exactly 3,772 records in it. > > I have tried the two approaches below to read it.....both fail to return > the last record before posting a '-1' to the file handle. Both return > exactly 3,771 records instead of 3,772 ?? > > First, the file is opened and one record read: Why? You don't need to read a record to verify that a file has been opened. > The above is followed by either of the two loops below: You're checking to see that a sequence is a sequence. Better would be to declare line as an object, then let Euphoria handle the reading:
object line integer fn fn = open("bcare_remit.txt","r") if fn < 1 then puts(1,"Error") abort(fn) else while 1 do line = gets(fn) if atom(line) then exit else -- do something with line end if end while end if close(fn)
Irv
5. Re: Looping thru file with 'gets()' fails to retrieve last record
- Posted by Bernard Ryan <xotron at bluefrog.com> Aug 05, 2004
- 451 views
John F Dutcher wrote: > > I have an Ascii text file in the cgi-bin with exactly 3,772 records in it. > > I have tried the two approaches below to read it.....both fail to return > the last record before posting a '-1' to the file handle. Both return > exactly 3,771 records instead of 3,772 ?? John: The first record in a file starts a zero offset. Are you sure you are counting from the first record. Bernie My files in archive: http://www.rapideuphoria.com/w32engin.zip http://www.rapideuphoria.com/mixedlib.zip http://www.rapideuphoria.com/eu_engin.zip http://www.rapideuphoria.com/win32eru.zip
6. Re: Looping thru file with 'gets()' fails to retrieve last record
- Posted by John F Dutcher <John_Dutcher at urmc.rochester.edu> Aug 06, 2004
- 454 views
I really like your code Irv....will try it. I've gotta start using that editor instead of Notepad... looks nice ! John D.
7. Re: Looping thru file with 'gets()' fails to retrieve last record
- Posted by irv mullins <irvm at ellijay.com> Aug 06, 2004
- 421 views
John F Dutcher wrote: > > I really like your code Irv....will try it. > > I've gotta start using that editor instead of Notepad... > looks nice ! Editor? Perhaps you are referring to the syntax colored code. That's a function of this listserver. If you want to post code, just surround the code with eucode markup. For example:
object x printf(1,"X equals %d",x)
To do this, use the word eucode surrounded with '<' and '>' symbols. End the code section with '<' /eucode '>' (Remove the spaces, of course. Let me try another way to post this: <eucode> just to see if it gets thru the listfilter function </eucode> Irv