1. Read & Write Binary Files

Hello,

I am trying to figure out what kind of code you would use to read and write 
binrary files, I have looked through the help files and through that "A
Beginners Guide to Euphoria"
but still I'm am confused, a little help?

new topic     » topic index » view message » categorize

2. Re: Read & Write Binary Files

Andy wrote:
> 
> Hello,
> 
> I am trying to figure out what kind of code you would use to read and write
> 
> binrary files, I have looked through the help files and through that "A
> Beginners
> Guide to Euphoria"
> but still I'm am confused, a little help?

Hi Andy

I guess it depends on what exactly you want to do; ie update parts of a binary
file, or just read a binary file and write a whole binary file, etc.

The trick is mainly in the open(...) statement; from the manual:

"r" - open text file for reading
"rb" - open binary file for reading
"w" - create text file for writing
"wb" - create binary file for writing
"u" - open text file for update (reading and writing)
"ub" - open binary file for update
"a" - open text file for appending
"ab" - open binary file for appending

So with binary files, it's a matter of making sure the "b" is in the second
argument to open.

The two key operations for binary files when updating in a random fashion are
seek() and tell().

seek() lets you position the place where you will do the next read or write
operation, and tell() lets you know where the current file position is.

Curiously enough I don't see a tell() in the Euphoria reference... I guess you
have to keep track of that yourself?

But yeah, without a specific problem to solve I'm at a bit of a loss to give
examples smile

Gary

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

3. Re: Read & Write Binary Files

I just need an example, anyone with how to read write and update a binary file
would be appericated

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

4. Re: Read & Write Binary Files

The only thing that is really different about binary files, is how you open them
and you can't or shouldn't use gets(). gets() is meant for reading in lines of
text and will remove certain characters. Binary files dont have 'lines'.

sequence data
integer fn,c
fn = open("file.dat","rb") -- open for binary reading
if fn != -1 then
  data = {}
  c = getc(fn)
  while c != -1 do -- until we reach EOF (-1)
    data &= c
    c = getc(fn)
  end while
  close(fn)
end if


Writing to a binary file is exactly the same as in text mode.


Chris Bensler
~ The difference between ordinary and extraordinary is that little extra ~
http://empire.iwireweb.com - Empire for Euphoria

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

5. Re: Read & Write Binary Files

Andy wrote:
> 
> I just need an example, anyone with how to read write and update a binary file
> would be appericated

Andy:

   To read bytes look at the library document under get_bytes Example.

  
Bernie

My files in archive:
WMOTOR, XMOTOR, W32ENGIN, MIXEDLIB, EU_ENGIN, WIN32ERU, WIN32API 

Can be downloaded here:
http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan

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

6. Re: Read & Write Binary Files

Ok I kinda of get it, how would I read write and update a file type I made 
something like this

constant FileType = {"Files","*.dat"}

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

7. Re: Read & Write Binary Files

On Sun, 26 Nov 2006 18:23:59 -0800, ags <guest at RapidEuphoria.com>
wrote:

>The two key operations for binary files when updating in a random fashion are
>seek() and tell().
>Curiously enough I don't see a tell() in the Euphoria reference... I guess you
>have to keep track of that yourself?
I think you meant where().

Regards,
Pete

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

8. Re: Read & Write Binary Files

Pete Lomax wrote:
> >Curiously enough I don't see a tell() in the Euphoria reference... I guess
> you have to keep track of that yourself?
> I think you meant where().

Ah, thanks for that.  That's a bit confusing, considering it's ftell() in C...
but I've obviously never needed to look it up anyway :)

Gary

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

9. Re: Read & Write Binary Files

On Mon, 27 Nov 2006 12:23:39 -0800, ags <guest at RapidEuphoria.com>
wrote:

>Pete Lomax wrote:
>> >Curiously enough I don't see a tell() in the Euphoria reference... I guess
>> you have to keep track of that yourself?
>> I think you meant where().
>
>Ah, thanks for that.  That's a bit confusing, considering it's ftell() in C... 
Well, just to dot the i's; basically (in windows anyway), afaict, both
where and ftell must ultimately be saving results from SetFilePointer,
at least for seek -1, so (cmiiw) both clib and/or eu backend do
manually keep track of it for the app developer. There really is no
such thing as a low-level windows API call Get FilePointer.

Not that you probably wanted to know.

Now I think about it, where() may not have been the best choice for
this function (neither is ftell), maybe get_file_position()?

Regards,
Pete

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

10. Re: Read & Write Binary Files

Pete Lomax wrote:
> 
> On Mon, 27 Nov 2006 12:23:39 -0800, ags <guest at RapidEuphoria.com>
> wrote:
> 
> >Pete Lomax wrote:
> >> >Curiously enough I don't see a tell() in the Euphoria reference... I guess
> >> you have to keep track of that yourself?
> >> I think you meant where().
> >
> >Ah, thanks for that.  That's a bit confusing, considering it's ftell() in
> >C...
> Well, just to dot the i's; basically (in windows anyway), afaict, both
> where and ftell must ultimately be saving results from SetFilePointer,
> at least for seek -1, so (cmiiw) both clib and/or eu backend do
> manually keep track of it for the app developer. There really is no
> such thing as a low-level windows API call Get FilePointer.
> 
> Not that you probably wanted to know.
> 
> Now I think about it, where() may not have been the best choice for
> this function (neither is ftell), maybe get_file_position()?
> 
> Regards,
> Pete
> 
It is true that Windows dows not have a GetFilePointer function but none is
needed. SetFilePointer returns the current file position. SetFilePosition allows
setting the position relative to the beginning, current position, or the file
end. Using an offset of zero will not alter the file position but will return the
current position. DOS has an similar function. I am quite certain that both C and
Euphoria will be using this method.

Larry Miller

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

11. Re: Read & Write Binary Files

On Tue, 28 Nov 2006 07:33:43 -0800, Larry Miller
<guest at RapidEuphoria.com> wrote:

>Pete Lomax wrote:
>> Well, just to dot the i's; 
>> There really is no
>> such thing as a low-level windows API call Get FilePointer.
>
>SetFilePointer returns the current file position.
Oh yeah! Thanks. I dot the i's; you cross the t's!
>I am quite certain that both C and Euphoria will be using this method.
Final thought: buffered i/o does mean they cannot, I think?!...

Regards,
Pete
PS: As I said, way more detail than anyone really needs to know.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu