Before You Post, Please Note...

-> Please Note: By Posting, you agree to submit the contents of your post to the Public Domain <- SEE: howto post to this blog

Friday, February 2, 2007

puppybasic intro ch. 09 - working with binary files

green text is being edited and should not be considered  
relevant to the tutorial.
for a while, it will make up
the bulk of this section.

black text has been edited to be more accurate with
regards to puppybasic.

........................................................................
working with binary files
1 of 7




an intro to puppybasic

chapter 9: working with binary files







........................................................................

........................................................................
working with binary files
2 of 7

it might be fun at this point to learn how to save and load some data in
a file, but this will be much more useful when you've learned about
decision making and loops. in the meantime, here is how to work with
binary files:

first of all, be sure to use this statement at the beginning of your
program:
DIM bt AS STRING * 1

so that you have a one byte string to work with. you can make longer
strings, but this one will work for any purpose.


........................................................................

........................................................................
working with binary files
3 of 7

always open the file :)

OPEN "filename.txt" FOR BINARY AS #1

if you are only going to read the file, you dont strictly need the bt
variable, and therefore don't need to DIM at the start of your program.
just INPUT a byte:

b$=INPUT$(1, #1)

and that's all there is to that. yes, it IS similar to keyboard input.


........................................................................

........................................................................
working with binary files
4 of 7

do it again to read the next byte. usually, you'll probably want to use
this method instead:

GET #1, q, bt
with q being the byte of the file you want to read... if q is 57, then
it will read the 57th byte of the file.

to write, use:
PUT #1, q, bt
where b is a string that's only one byte (or letter) long. it's pretty
much the same idea. some useful functions for working with files are:


........................................................................

........................................................................
working with binary files
5 of 7

q = FREEFILE

sets q to the number of the next filenumber that's free... if you've
opened a file as #1, and a second file, as #2, then FREEFILE will equal
3.

LOC(n)

is the byte you're set to read or write at. if you've used INPUT$(1, #1)

50 times already, LOC(1) will be 51.


........................................................................

........................................................................
working with binary files
6 of 7

LOF(n)

is worth remembering, LOF(n) is equal to the length of file #n, if you
open a file as #1 and the LOF(1) is 0, that file is empty.

always close the file when you're done working with it:

CLOSE #1

if you are only working with one file, or if you don't have any other
files that you need to leave open, using close with no file numbers:


........................................................................

........................................................................
working with binary files
7 of 7

CLOSE

will close all files.


-= end of chapter 9 =-







........................................................................
an intro to puppybasic (chapter 9)
this content is public domain.
........................................................................


click here to go back to the contents page

0 comments: