Page 1 of 1

Opening, reading, and writing Binary files

Posted: Tue Feb 22, 2005 2:51 pm
by Levi
I need to develop some routines for creating a Binary file in use of creating my own game file type. And I need some indepth info on the use of Binary files.

Although I am weary about using binary files because if some data needs to be entered into the middle, does it accept placing data in a file without overwriting data? That sort of thing.

Posted: Tue Feb 22, 2005 3:00 pm
by {Nathan}
You can add data without overwriteing by opening AS APPEND. Thats all I know. :?

Posted: Tue Feb 22, 2005 3:17 pm
by Mitth'raw'nuruodo
Nice indepth Nate.
Binary Files are right up my alley.
To make/open files you:
OPEN "filename.extention" FOR verbname AS #number

filename.extention is the file your opening.

verbname is three ways to OPEN a file.
verbname can be OUTPUT, INPUT, APPEND.
OUTPUT-> clears all contents in a file if filename.extention already exists. If it doesn't it creats a new file. This mode alows you to write to the file.
INPUT-> This is a Read-only mode. You may get stuff out of this file.
APPEND-> This file opens a file like output but doesn't erase or make a new file. You may write to this file. ALL data you write just gets attached to the end of the file.

number is the number your assigning to the open file as a refernce for you later.

to close a file you must do:
CLOSE #number

WEll that's it.

look up: INPUT #, PRINT#, EOF(), LOF(), and all SEE ALSOs with those look ups.

Posted: Tue Feb 22, 2005 3:53 pm
by MystikShadows
Nice indepth Mitt but what I'm reading there is all about sequential files ;-). From what I read way back when, binary files work with put #, get #, seek # to position yourself where you want, etc etc....like Random Access files.

Posted: Tue Feb 22, 2005 5:04 pm
by {Nathan}
Check the random file accsess from QBXP. Also at www.pure-text.com :wink:
:twisted:

Posted: Tue Feb 22, 2005 6:18 pm
by Mitth'raw'nuruodo
I always get sequntial files, binary files, and random access files confused. What Nate was typing about: AS APPEND I assumed levi was talking about the OPEN FOR stuff...

But oh well...Unfortunaly I know a lot about sequntial files but not about the others. Sorry Levi. Thanks MystikShadows for reconizing my error. :D

Posted: Tue Feb 22, 2005 6:21 pm
by Levi
That's alright. I use to get them confused too until I had get# and put# shoved into my brain. But my original help files all disappeared. So I could only do Sequential, and though this isn't bad, it's not the best for creating a game file format.

Posted: Tue Feb 22, 2005 6:49 pm
by {Nathan}
BTW, hi levi! Havn't seen you here in a LONG time :evil:
Now, the tourcher begins!
:twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted:

Posted: Tue Feb 22, 2005 6:57 pm
by Levi
Indeed. I'm extremely busy these days. I've got a book to write. Ten tutorials suggested by different people. Programs for three companies including a chat program in C++. I'm just too swamped to come here often. This is for fun, a relaxation thing.

Posted: Tue Feb 22, 2005 9:03 pm
by Mitth'raw'nuruodo
Hey I do C++!

But anyway, don't what Z!re to get mad at me again....


Why is it hard creating your own game file type with sequential files?
I think its easy, you just got to make your own commands that you put in the file to identify the stuff you want. I use negitive numbers as commands because I rarely use them to store info.

ie. -2 could mean the end of a set of numbers that deal with tile locations in a room. -3 could mean the end of a row of tiles in a room. You could also put sepcific tile pixel locations in it and have -1 be the end of a tile pixel data sequence. Before I knew of BSAVE/BLOAD I used squentail files to store my pics. I use them to store specific levels or bosses. And Attributes for certain things. Also keyboard controls too.

There are many things you can do with sequential files. Just learn FOR INPUT, INPUT #, and EOF() and you should learn it pretty easyly....

Mabey I'll offer Pete to do a Tut on sequential file with game design on them. Just a thought Pete, I'm really busy right now.

Posted: Tue Feb 22, 2005 9:21 pm
by Levi
Well in truth they are easy with Sequential files. And a little difficult with Binary. But Binary files are smaller. If you think about it, you can grab an entire string, and I mean the length of a string variable, by going Input #1, astring

Or is it astring=Input#1? No big deal.

But you're wasting space. With Binary you supply the size of the string. dim a as string*2

Integers are always integers. You say Get #1, intNumber

Doesn't matter if it's a 1 or a two it gets the number. No more converting from string to number using Val. It's just right there. All in all it's the best way to go about it.