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. 13 - working with text 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 text files
1 of 8




an intro to puppybasic

chapter 13: working with text files







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

........................................................................
working with text files
2 of 8

working with text files is a bit different than working with binary
files... for one, the way you open the file depends on whether you will
be reading or writing. for this reason and a few others i prefer to
handle text files as if they were binaries and open them for binary
file access. you may find this is easier when you're starting out, you
may not. often i am too lazy and instead open text files as text files.
to print the following lines to a file:
this is a line. skip the next one:

okay, this is another line...
this is, too.


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

........................................................................
working with text files
3 of 8

always OPEN the file:

OPEN "filename.txt" FOR OUTPUT AS #1

if you do it this way it will start from scratch... that is,
"filename.txt" will be BLANKED if it wasn't before. for this reason you
will use APPEND for now. APPEND, instead of erasing the file, adds to
the end of it. if you are writing to a log, this is exactly what you
want. if you are only storing the data for a saved game file, this may
be problematic. in that case you may prefer OPEN... FOR OUTPUT.
regardless:


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

........................................................................
working with text files
4 of 8

OPEN "filename.txt" FOR APPEND AS #1

now we'll use the PRINT statement to write our lines to the file. the
file currently open is #1, so we'll use PRINT #1, q$

PRINT #1, "this is a line. skip the next one:"
PRINT #1, ""
PRINT #1, "okay, this is another line..."
q$ = "this is, too."
PRINT #1, q$



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

........................................................................
working with text files
5 of 8

the last line was printed differently to demonstrate again that a string
is, after all, a string, and that you can PRINT #1, q$ as well as PRINT
an expression in quotes. both of these are examples of valid strings.

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:
CLOSE
will close all the files your program has opened.


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

........................................................................
working with text files
6 of 8

now to read the file, we open it again, this time for input:
OPEN "filename.txt" FOR INPUT AS #1

the text file is read one line at a time. one of the most reliable ways
to do this is to use line input from within a loop:








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

........................................................................
working with text files
7 of 8

DO: LINE INPUT #1, q$

PRINT q$ 'print the line to the screen

the program has to know when to stop looping. EOF(1) is a condition that
will be true when the end of the file is reached. it can end the LOOP
this way:
LOOP UNTIL EOF(1)

you may find This LOOP superior for reading text files:
DO WHILE EOF(1) = 0: LINE INPUT #1, q$: LOOP 'it handles 0-length files.


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

........................................................................
working with text files
8 of 8

if you only wanted to read the first line of course, the loop isn't
needed, only line input #1, q$

close the file when you're done with it. no files are open that need to
stay open, so it's alright to just say:

CLOSE


-= end of chapter 13 =-



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


click here to go back to the contents page

17 comments:

Anonymous said...

generic xanax xanax effects anxiety - xanax effects kidneys

Anonymous said...

xanax online clonazepam vs. xanax for anxiety - cost of generic xanax

Anonymous said...

alprazolam mg xanax 2089 - prozac vs xanax for anxiety

Anonymous said...

generic xanax generic xanax yellow - xanax dosage by pill

Anonymous said...

xanax online xanax blue pill - xanax 44386

Anonymous said...

tramadol 50mg buy-cheap-tramadol.org - buy tramadol cod delivery

Anonymous said...

buy tramadol online buy tramadol online no prescription cheap - good place buy tramadol online

Anonymous said...

buy cheap cialis cialis online satis - buy cialis online in south africa

Anonymous said...

cheap cialis generic cialis rx - cialis usage

Anonymous said...

cialis online best website order cialis - buy-cialis-generic-online

Anonymous said...

http://landvoicelearning.com/#30896 tramadol constipation - tramadol for dogs bone cancer

Anonymous said...

buy tramadol online tramadol dosage opiate withdrawal - tramadol hcl drug test

Anonymous said...

tramadol online i took 7 tramadol - tramadol 100mg flashback

Anonymous said...

buy tramadol without rx tramadol overdose itching - tramadol online pharmacy

Anonymous said...

http://reidmoody.com/#53287 ativan withdrawal after 2 weeks - 1mg lorazepam diazepam equivalent

Anonymous said...

buy ativan online ativan 774 - many 1mg lorazepam get high

Anonymous said...

http://ranchodelastortugas.com/#50238 generic xanax pills - xanax effects length