1. file comparing
I wrote this little program a little while back, because I needed
something that worked like the old DOS program, compare. This works
great, until you try to compare two large files (I assume because the
program runs out of memory). Is there any way I can avoid this?
I know I could use getc(), but wouldn't that also be really slow, for
large files?
Also, I know I could use getc() to maybe return the exact bytes that are
different, but how would I go about that?
---------------includex.e-----------------
include get.e
sequence input_data1, input_data2, cmd, path
integer file1, file2
cmd = command_line()
if length(cmd) < 3 then
puts( 1, "Usage: comparex filename1 filename2\n")
abort(0)
end if
puts( 1, "\nComparing the following files:\n")
path = {}
for i = 3 to length(cmd) do
path = append( path, cmd[i] )
printf( 1, "File %d: %s\n", { i-2, path[i-2]})
end for
file1 = open(cmd[3], "rb")
file2 = open(cmd[4], "rb")
if file1 = -1 and file2 = -1 then
puts(1,"\nError opening files\n\n")
end if
input_data1 = gets(file1)
input_data2 = gets(file2)
if compare(input_data1,input_data2) = 0 then
puts(1, "\n These files are the same\n")
else
puts(1, "\n These files are not the same\n")
end if
close(file1)
close(file2)
--
Greg Phillips
i.shoot at rednecks.com
http://euphoria.server101.com
--
Useless fact of the day:
If you keep a Goldfish in a dark room it will eventually turn white
2. Re: file comparing
-----Original Message-----
From: Greg Phillips <i.shoot at REDNECKS.COM>
To: EUPHORIA at LISTSERV.MUOHIO.EDU <EUPHORIA at LISTSERV.MUOHIO.EDU>
Date: Sunday, January 03, 1999 3:12 PM
Subject: file comparing
>I wrote this little program a little while back, because I needed
>something that worked like the old DOS program, compare. This works
>great, until you try to compare two large files (I assume because the
>program runs out of memory). Is there any way I can avoid this?
>
>I know I could use getc(), but wouldn't that also be really slow, for
>large files?
Why don't you use getc() to read in chunks of data (from each file
individually), say 100k to 500k, compare those and then get the next chunks
from the files and so on on?
According to library.doc the length of an open file can be determined by :
file_length = seek(file_id, -1 )
You may need this info to implement this suggestion.
michael at igrin.co.nz
3. Re: file comparing
- Posted by JJProg at CYBERBURY.NET
Jan 03, 1999
EU>I wrote this little program a little while back, because I needed
EU>something that worked like the old DOS program, compare. This works
EU>great, until you try to compare two large files (I assume because the
EU>program runs out of memory). Is there any way I can avoid this?
EU>I know I could use getc(), but wouldn't that also be really slow, for
EU>large files?
EU>Also, I know I could use getc() to maybe return the exact bytes that are
EU>different, but how would I go about that?
You could use this to compare two files, but it won't tell which bytes
are different. It would be fairly easy to modify it, though. It uses
doswrap.e to read the files really quickly.
include doswrap.e
include machine.e
atom input_data1, input_data2
sequence cmd, path
integer file1, file2, read1, read2, min
cmd = command_line()
if length(cmd) != 4 then
puts(1,"Usage: comparex filename1 filename2\n")
abort(0)
end if
puts(1,"\nComparing the following files:\n")
path = {}
for i = 3 to length(cmd) do
path = append(path,cmd[i])
printf(1,"File %d: %s\n",{i-2,path[i-2]})
end for
file1 = DosOpen(cmd[3],READ)
file2 = DosOpen(cmd[4],READ)
if file1 = -1 or file2 = -1 then
puts(1,"\nError opening files\n")
abort(0)
end if
input_data1 = allocate_low(500)
input_data2 = allocate_low(500)
while 1 do
read1 = BlockRead(file1,input_data1,500)
read2 = BlockRead(file2,input_data2,500)
if read1 = 0 and read2 = 0 then
exit
end if
if read1 < read2 then
min = read1
else
min = read2
end if
for i = 0 to min-1 do
if peek(input_data1+i) != peek(input_data2+i) then
puts(1,"The files are not the same\n")
min = -1
exit
end if
end for
if read1 != read2 then
puts(1,"The files are not the same length\n")
min = -1
end if
if min = -1 then
exit
end if
end while
if min != -1 then
puts(1,"The files are the same\n")
end if
min = DosClose(file1)
min = DosClose(file2)
free_low(input_data1)
free_low(input_data2)
Jeffrey Fielding
JJProg at cyberbury.net
http://members.tripod.com/~JJProg/