Opening, reading, and writing Binary files

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Opening, reading, and writing Binary files

Post 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.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

You can add data without overwriteing by opening AS APPEND. Thats all I know. :?
Image
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Post 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.
"But...It was so beutifully done"
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post 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.
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Check the random file accsess from QBXP. Also at www.pure-text.com :wink:
:twisted:
Image
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Post 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
"But...It was so beutifully done"
User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Post 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.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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:
Image
User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Post 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.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Post 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.
"But...It was so beutifully done"
User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Post 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.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer
Post Reply