1. Reading a binary file

In the example for 'get_bytes', it shows how to read a binary file 100
bytes at a time and when then length of the returned sequence is less than
100, the end of the file has been reached:

while 1 do
    chunk = get_bytes(fn, 100) -- read 100 bytes at a time
    whole_file &= chunk        -- chunk might be empty, that's ok
    if length(chunk) < 100 then
        exit
    end if
end while

Is there an easier/faster way to read in a binary file to a sequence in one
shot?

Thanks,
Brian

new topic     » topic index » view message » categorize

2. Re: Reading a binary file

------=_NextPart_000_0052_01C007B1.17656260
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Brian Broker wrote:

Is there an easier/faster way to read in a binary file to a sequence in =
one
shot?

I asked the same question a long time ago,
the very best I could come up with is on the archive listed
as,=20
Fast Load of Bitmap Images by Wayne Overman
dated Feb-13-00  The theory I use is very close to what you have written =
in the E-mail take a look.=20
euman at bellsouth.net


------=_NextPart_000_0052_01C007B1.17656260
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4134.600" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Brian Broker wrote:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Times New Roman" =
size=3D3>Is there an=20
easier/faster way to read in a binary file to a sequence in=20
one<BR>shot?</FONT><BR></FONT><FONT face=3DArial size=3D2></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I&nbsp;asked the same question a long =
time=20
ago,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>the very best I could come up with is =
on the=20
archive&nbsp;listed</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>as, </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Fast Load of Bitmap Images by Wayne=20
Overman</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>dated Feb-13-00&nbsp;&nbsp;The theory I =
use is very=20
close to what you have written in the E-mail take a look. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2><A=20

------=_NextPart_000_0052_01C007B1.17656260--

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

3. Re: Reading a binary file

On Wed, 16 Aug 2000, you wrote:
> In the example for 'get_bytes', it shows how to read a binary file 100
> bytes at a time and when then length of the returned sequence is less than
> 100, the end of the file has been reached:
>
> while 1 do
>     chunk = get_bytes(fn, 100) -- read 100 bytes at a time
>     whole_file &= chunk        -- chunk might be empty, that's ok
>     if length(chunk) < 100 then
>         exit
>     end if
> end while
>
> Is there an easier/faster way to read in a binary file to a sequence in one
> shot?

The number (100) can be anything you want - 1000 or 50000, whatever.
When length(chunk) < that number, you've finished.
Probably the most efficient number would match the size of the input buffer (?)

Irv

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

4. Re: Reading a binary file

On Wed, 16 Aug 2000 16:50:51 -0400, Brian Broker <bkb at CNW.COM> wrote:

>In the example for 'get_bytes', it shows how to read a binary file 100
>bytes at a time and when then length of the returned sequence is less than
>100, the end of the file has been reached:
>
>while 1 do
>    chunk = get_bytes(fn, 100) -- read 100 bytes at a time
>    whole_file &= chunk        -- chunk might be empty, that's ok
>    if length(chunk) < 100 then
>        exit
>    end if
>end while
>
>Is there an easier/faster way to read in a binary file to a sequence in one
>shot?
>

Brian:

   DOSWRAP.E by Jacques Deschenes has a disk BLOCKREAD function.

Bernie

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

5. Re: Reading a binary file

Brian,

Every time I trip over the get_bytes() function, I get a feeling
Robert wrote it in a darker moment of his life, included it in get.e
when he was not quite himself, and he has not looked at it since...

When I want maximum speed, I determine the size of the file first,
create a buffer and read the bytes using a simple for loop:

----------------------------------------------------------------------
    include file.e

    sequence buffer
    integer fi,len,o

    fi = open(inFile, "rb")
    if fi = -1 then
        puts(1, "Input file " & inFile & " not found!")
        abort(1)
    end if

    o = seek(fi,-1)         -- go to end of input file
    len = where(fi)         -- get length of input file in bytes
    o = seek(fi,0)          -- go back to beginning of input file
    buffer = repeat(0, len) -- initialize your buffer
    for i=1 to len do
        buffer[i] = getc(fi)
    end for
    close(fi)
----------------------------------------------------------------------

jiri

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

6. Re: Reading a binary file

On Thu, 17 Aug 2000 12:10:29 +1200, Jiri Babor wrote:

>Brian,
>
>Every time I trip over the get_bytes() function, I get a feeling
>Robert wrote it in a darker moment of his life, included it in get.e
>when he was not quite himself, and he has not looked at it since...
>
>When I want maximum speed, I determine the size of the file first,
>create a buffer and read the bytes using a simple for loop:
>
>----------------------------------------------------------------------
>    include file.e
>
>    sequence buffer
>    integer fi,len,o
>
>    fi = open(inFile, "rb")
>    if fi = -1 then
>        puts(1, "Input file " & inFile & " not found!")
>        abort(1)
>    end if
>
>    o = seek(fi,-1)         -- go to end of input file
>    len = where(fi)         -- get length of input file in bytes
>    o = seek(fi,0)          -- go back to beginning of input file
>    buffer = repeat(0, len) -- initialize your buffer
>    for i=1 to len do
>        buffer[i] = getc(fi)
>    end for
>    close(fi)
>----------------------------------------------------------------------
>
>jiri

Thanks jiri,

After looking at what 'get_bytes' actually does, I think your routine
should be in 'get.e'
I think most of the time you read a binary file, you want the whole thing
in a sequence so you can work with it...

Maybe Rob will consider this for the next release?

-- Brian

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

7. Re: Reading a binary file

Hy,

I actually do not see what's the problem with speed in
retreiving data from files, I always use this:

integer hnd,char
sequence filestr
filestr=""
hnd=open("file.exe","rb")
if hnd !=-1 then
char=getc(hnd)
while char !=-1 do
  filestr&=char
  char=getc(hnd)
end while
close(hnd)
end if

If you have a large file, it does cost a lot of time because of the
"filestr&=char" instruction, but which files are that big.

Or if you really want speed, try to use this information for an
asm/machlang program.
INT 21 3D-- - DOS 2+ - "OPEN" - OPEN EXISTING FILE
INT 21 42-- - DOS 2+ - "LSEEK" - SET CURRENT FILE POSITION
INT 21 3F-- - DOS 2+ - "READ" - READ FROM FILE OR DEVICE
INT 21 48-- - DOS 2+ - ALLOCATE MEMORY (LowMem)
INT 21 49-- - DOS 2+ - FREE MEMORY
INT 31 FF0B - CauseWay - "GetMem" - ALLOCATE BLOCK OF MEMORY
INT 31 FF0C - CauseWay - "GetMem32" - ALLOCATE BLOCK OF MEMORY (32-bit)
INT 31 FF0F - CauseWay - "RelMem" - RELEASE PREVIOUSLY ALLOCATED MEMORY
INT 21 3E-- - DOS 2+ - "CLOSE" - CLOSE FILE

Open the file, seek the end (Can't find a getfilesize function),
calculate the size, set to beginning
allocate mem, read till size, close file, DoSomething, freemem.


Bye,
PQ

>From: Jiri Babor <J.Babor at GNS.CRI.NZ>
>Every time I trip over the get_bytes() function, I get a feeling
>Robert wrote it in a darker moment of his life, included it in get.e
>when he was not quite himself, and he has not looked at it since...
>
>When I want maximum speed, I determine the size of the file first,
>create a buffer and read the bytes using a simple for loop:


________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

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

8. Re: Reading a binary file

PQ wrote:

>I actually do not see what's the problem with speed in
>retreiving data from files, I always use this:
>
>integer hnd,char
>sequence filestr
>filestr=""
>hnd=open("file.exe","rb")
>if hnd !=-1 then
>char=getc(hnd)
>while char !=-1 do
>  filestr&=char
>  char=getc(hnd)
>end while
>close(hnd)
>end if
>
>If you have a large file, it does cost a lot of time because of the
>"filestr&=char" instruction <snip>

I actually do not see what *your* problem is, since you almost answered
your own question. Almost. Your code also has an extra test (char !=
-1) and an extra assignment (char = getc(hnd)). It all adds up to a
lot of unnecessarily wasted time, especially if you are in a hurry.

> ... but which files are that big.

Well, what do I say...

PQ then pasted about a dozen lines of interrupt info from some
"PC Programming for Beginners" and offered the following advice:

>Open the file, seek the end (Can't find a getfilesize function),
>calculate the size, set to beginning allocate mem, read till size,
>close file, DoSomething, freemem.

Contrast that with my stupid suggestion! Except PQ is, presumably,
also poking the read values into memory, and these will have to be
somehow retrieved later. Unless, of course, PQ has already forgotten
the original Brian's question : "Is there an easier/faster way to read
in a binary file to a *sequence* in one shot?" (my emphasis)

PQ, I hope I have answered your questions, but if it is just an
argument, that you want, forget it.

jiri

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

9. Re: Reading a binary file

jiri, be nice.

PQ wrote:

> >I actually do not see what's the problem with speed in
> >retreiving data from files, I always use this:
> >

jiri responded, in part:

> I actually do not see what *your* problem is, since you almost answered
> your own question. Almost. Your code also has an extra test (char !=
> -1) and an extra assignment (char = getc(hnd)). It all adds up to a
> lot of unnecessarily wasted time, especially if you are in a hurry.


__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

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

Search



Quick Links

User menu

Not signed in.

Misc Menu