[futurebasic] Re: [FB] Write & Read files

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 2005 : Group Archive : Group : All Groups

From: Stu Cram <stu@...>
Date: Thu, 22 Dec 2005 15:18:36 -0600
On Dec 20, 2005, at 8:29 PM, <bheibert@...> wrote:
> Hi,
> I know how to dislay open & save dialog boxes
> but I don't know how to open a text file or save a text file
> I don't have the manuals with me I am in Mexico :-)
> I looked at the online help and all I saw was
>   filename$ = FILES$(_fOpem,"Open file...",refNumVar%)
> Sorry
> Brian
> -------------------------------------

You've looked at just one instruction.
Also look at the OPEN, PRINT#, INPUT#, LINE INPUT# and CLOSE 
instructions.

It's similar to using a book on a shelf.
	a) select the file:		FILES$
	b) open the file:		OPEN
	c) write in the file:	PRINT#
        or read the data:	INPUT# or LINE INPUT#
	d) close the file:		CLOSE


A file can be open for reading or writing but not both.
You must close a file before re-opening it.

A TEXT file is the simplest type to work with.
It has just lines of text ending with a 'return'.
The lines of text are written or read sequentially, one after another.
To keep your program simple, just write or read one item per line
Each item may be a number of a text value.
Use PRINT# to write an item (either text or a number)
Use INPUT# to read a numeric item
Use LINE INPUT# to read a text value
    (to get entire line including any commas, semi-colons, etc. )
    (INPUT# just goes to first comma and skips leading/trailing spaces)
When you input data items, you must use a variable name but
    it need not be the same name as when the file was written.

Sample program:  Creates a file in the same folder as your program."
See previous example for demo with FILES$() to select any file.

'======================================================
' TEXT file demo for BH;   FB digest Dec 23, 2005
DIM AS DOUBLE num1, num2
DIM AS STR255 txt1, txt2
DIM AS STR255 filename

num1 = 14.758
txt1 = "  Pistachio, Chocolate, and Tutti-Fruiti     "

WINDOW 1, "Quick Text File Demo", (0,0)-(500,500), _doc
TEXT _Courier, 12, 0

' Saving a file...
filename$ = "Demo Text File"
DEF OPEN "TEXTttxt"  ' set file type & creator code
OPEN "O", 1, filename
   PRINT #1, num1
   PRINT #1, txt1
CLOSE #1
PRINT "---FILE SAVED---"
PRINT

' Reading the same file for this demo
OPEN "I", 2, filename
   INPUT #2, num2
   LINE INPUT #2, txt2
CLOSE #2
PRINT "---FILE OPENED AND READ BACK IN---"
PRINT "Contents of file: "; filename
PRINT "Line 1:    num2  = <"; num2; ">"
PRINT "Line 2:    txt2  = <"; txt2;">"

PRINT : PRINT "Press Command-Q to quit..."
DO
   HandleEvents
UNTIL 0
'============================================

So what are the typical errors to watch out for.
1.  File not found:  Unknown file name when trying reading a file.
2.  Using INPUT # instead of LINE INPUT # for entire text lines.
3.  Using the wrong file number - Set the file number in the OPEN
      and use it in the following INPUT # or PRINT # and CLOSE # 
instructions
     A different number can be used later one when opening another file.
4.  End-of-file:  You can't read past the last line of a file.
	See the EOF() function to test for the end of a file of unknown length.

BTW, There are other file structures that might be more efficient for
transferring large blocks of data to/from files but that's another 
lesson.
KISS for now - Keep it short and simple whenever possible.
BTW2, writing data into a text file is a handy way to transfer it to 
other
programs like word processors and spreadsheets.

Hope this also helps you get started with text files.
Merry Xmas and Happy New Year
-Stu