1. Writing To Binary File

Hey guys,

I'm confused as how I write to a binary file, is it the same as you would to a text file or is it different? Also, how would I do under a windows program using the win32lib?

integer fn 
integer data 
data = 0 
fn = open("sav.dat","wb") 
 
if fn = -1 then 
    puts(1,"Cannot write file!\n") 
  else 
      print(fn,data)  
end if 

Or would it be like this?

integer fn 
integer data 
data = 0 
fn = open("sav.dat","wb") 
 
if fn = -1 then 
    puts(1,"Cannot write file!\n") 
   else 
       wPrint(fn,data) 
end if 

Any help, please

new topic     » topic index » view message » categorize

2. Re: Writing To Binary File

The print() routine outputs a plain-text, semi-human-readable representation of a Euphoria object. If you're writing files in binary mode, you should be puts()-ing a sequence of bytes to the file. Typically, when you're using binary mode, you'll be using some sort of defined file structure, either an existing format (like an image or a document), or your own custom layout (like EDS). If you want to print() object to a file, then use text mode and later read them back with get().

-Greg

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

3. Re: Writing To Binary File

ghaberek said...

The print() routine outputs a plain-text, semi-human-readable representation of a Euphoria object. If you're writing files in binary mode, you should be puts()-ing a sequence of bytes to the file. Typically, when you're using binary mode, you'll be using some sort of defined file structure, either an existing format (like an image or a document), or your own custom layout (like EDS). If you want to print() object to a file, then use text mode and later read them back with get().

-Greg

OK, I'm getting a little bit more of it, but my code still returns an error whenever I try to run it. It dosen't function correctley. Here's what I'm trying to do.

integer fn 
fn = open("sav.frst","wb") 
if fn = -1 then then 
   puts(1,"Cannot write file!\n") 
  else 
      wPrint(fn,HP) 
end if 

However when whenver I do that, my program crashes with a console window comes up and has a bunch of errors in it, also I'm using the win32lib, so I am wondering what I am doing wrong

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

4. Re: Writing To Binary File

Andy said...

OK, I'm getting a little bit more of it, but my code still returns an error whenever I try to run it. It dosen't function correctley. Here's what I'm trying to do.

integer fn 
fn = open("sav.frst","wb") 
if fn = -1 then then 
   puts(1,"Cannot write file!\n") 
  else 
      wPrint(fn,HP) 
end if 

However when whenver I do that, my program crashes with a console window comes up and has a bunch of errors in it, also I'm using the win32lib, so I am wondering what I am doing wrong

I assume you are not strictly needing a binary file, but just one in which you can write and read arbitary Euphoria objects. In which case, use the 'print' routine.

integer fn 
fn = open("sav.frst","wb") 
if fn = -1 then then 
   puts(1,"Cannot write file!\n") 
  else 
      print(fn,HP) 
end if 

This actually writes the data out as human-readable text rather than in strict binary format, but if that isn't an issue you should be fine. Use the get() function to read the data back in.

If you really need a pure binary format, then you might need to roll-your-own or use the EDS library. There will be a binary form of print()/get() available in the v4.0 release.

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

5. Re: Writing To Binary File

Andy said...

However when whenver I do that, my program crashes with a console window comes up and has a bunch of errors in it, also I'm using the win32lib, so I am wondering what I am doing wrong

I see, the main problem is that you're using wPrint() which is for printing data onto windows, not to files.

-Greg

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

6. Re: Writing To Binary File

I see, thank you for your help.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu