1. Looping thru file with 'gets()' fails to retrieve last record

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


new topic     » topic index » view message » categorize

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

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 message » categorize

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

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

new topic     » goto parent     » topic index » view message » categorize

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

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

new topic     » goto parent     » topic index » view message » categorize

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

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

new topic     » goto parent     » topic index » view message » categorize

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

I really like your code Irv....will try it.

I've gotta start using that editor instead of Notepad...
looks nice !

John D.

new topic     » goto parent     » topic index » view message » categorize

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

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: &lt;eucode&gt; just 
to see if it gets thru the listfilter function &lt;/eucode&gt;

Irv

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu