Re: Looping thru file with 'gets()' fails to retrieve last record

new topic     » goto parent     » topic index » view thread      » older message » newer message

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

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu