1. Writing To Binary File
- Posted by Andy Mar 08, 2009
- 975 views
- Last edited Mar 09, 2009
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
2. Re: Writing To Binary File
- Posted by ghaberek (admin) Mar 08, 2009
- 926 views
- Last edited Mar 09, 2009
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
3. Re: Writing To Binary File
- Posted by Andy Mar 09, 2009
- 953 views
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
4. Re: Writing To Binary File
- Posted by DerekParnell (admin) Mar 09, 2009
- 941 views
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.
5. Re: Writing To Binary File
- Posted by ghaberek (admin) Mar 09, 2009
- 893 views
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