Re: Writing to a file

Scott Raney (raney@metacard.com)
Thu, 13 Feb 1997 21:52:26 -0700 (MST)

On Thu, 13 Feb 1997, Simon Lord wrote:

> I'm having some trouble writing text to a file. I've been able to read
> in files, edit them etc but when it comes to writing these changes back
> into the file it just doesn't seam to want to go. I get the error "file
> not open", but as you can see from the exact script below I seem to be
> following the rules. It also says "Bad Token: matl.db" ??????
>
> I've tried writing fields, single words and variables like the one shown
> below to a file without success.
>
> >open file "matl.db" for read
> >read from file "matl.db" until eof -- I put this in as a sign of desperation, I needed to kow if it was reading it, which it was.
> >write "GLOBAL " & item 2 of line x of cd fld "shellfield" & tabForm & ": MATL { 1 }" to file "matl.db"
> >close file "matl.db"

Umm, if you open the file for *read*, you can't write to it. You want
"open file x" or "open file x for update" (both of which opens it for
both read *and* write) or "open file x for write" or "open file x for
append". Also note that after reading from the file until eof, the
file pointer is at the end of the file, meaning all writes will extend
the file rather than overwriting the data in the file.

Finally, you should check the result after opening a file: if there
was an error (bad file name, or no permission to open the file for
that mode), it'll tell you.
Scott

> Any help is much appreciated.
> --
> [---------------------------] Simon Lord
> [Important letters that.....] Database Engineer, CAE Electronics
> [contain no errors will.....] tel: (514) 341-2000 x3861
> [develop errors in the mail.] fax: (514) 340-5496
> [---------------------------] email: simonl@cae.ca
>

***************************************************************
Scott Raney raney@metacard.com http://www.metacard.com
Tcl and ksh: syntactic gymnastics
MetaCard: it does what you think