Bug with where()?

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

Hello Rob, hello all,

recently I wrote a program to patch a .mbx file (the outbox of my
e-mail program). I decided to open the .mbx file as text file for
update. The principle of the program is demonstrated by "pets.ex(w)"
and "pets.txt" below.

When the program is running, something strange happens. It says:
"replaced 'dog' 6 times with 'cat'", but 'dog' only existed 2 times in
"pets.txt". Furthermore, "pets.txt" grows, from originally 60 bytes to
more than 700 bytes.
After activating the command flush(f), the program works as expected,
but I think it also should work correctly without flush(f).

It looks like a bug concerning to where(), similar to what has already
been fixed (described in the most recent section of relnotes.htm).

Regards,
   Juergen



--======================================================================
file pets.ex or pets.exw
------------------------------------------------------------------------
include file.e

object line
atom file_pos
integer f, p, replaced

f = open("pets.txt", "u")
replaced = 0
while 1 do
   file_pos = where(f)
   line = gets(f)
   if atom(line) then exit end if

   printf(1, "%d, ", {file_pos})
   p = match("dog", line)
   if p then
      line[p..p+2] = "cat"
      if seek(f, file_pos) then
         puts(1, "Cannot set the file pointer")
         abort(1)
      end if
      puts(f, line)
      -- flush(f)       -- !!
      replaced += 1
   end if
end while
close(f)
printf(1, "%d\n\n", {file_pos})
printf(1, "replaced 'dog' %d times with 'cat'\n\n", {replaced})
puts(1, "Press Enter...")
p = getc(0)
--======================================================================


--======================================================================
file pets.txt
------------------------------------------------------------------------
Galahad (dog)
Garbo (fish)
Garfield (dog)
Geesha (bird)
--======================================================================

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

Search



Quick Links

User menu

Not signed in.

Misc Menu