Reading and writing temporary files 02

Last time I looked at two programs: one creates a temporary file and the other one reads it.

The two programs here are an improvement.  They contain some error checking for the case when it is not possible to read and or write to the temp folder for whatever reason.  The second program also deletes the temporary file once it has read from it (this is a good idea so you don't fill up your Temp folder with lots of garbage).  Finally, the temp file can contain any number of lines of text.  Each line is stored in the text file as separated by double forward slash characters //.

You might also be interested in reading a text file byte by byte.

Code for program one:

     REM this program writes a
     REM temporary text file

text$ = ""
     in$ = CHR$(255)
     PRINT "Enter text at prompt. Press enter to finish."
     WHILE in$ <> ""
       INPUT ">" in$
       IF in$<> "" text$ +=  (in$ + "//" )

REM create the file
PRINT "writing temp file"
     errmessage$ = ""
     IF NOT(FN_createTempFile( "mytemp.tmp", text$, errmessage$ ) ) THEN

DEFFN_createTempFile( filename$, text$, RETURN message$ )
     REM writes the text into the temp file whose
     REM filename is passed in filename$
ON ERROR LOCAL message$ = "Cannot create temp file.": ENDPROC
     file% = OPENOUT( @tmp$+filename$ )
     PRINT#file%, text$
     = TRUE

Code for program two:

      REM this program reads from a
     REM temp text file of any length
     REM and deletes it after use

INSTALL @lib$+"stringlib"

     filename$ = "mytemp.tmp"
     PRINT "looking for the text file."
     errmessage$ = ""
     text$ = FN_readTempFile( filename$, errmessage$)
     IF errmessage$ = "" THEN
REM finished with temp file so delete it
IF NOTFN_deleteTempFile( filename$, errmessage$ ) THEN
"Temp file deleted"
         PRINT "file contains:"
         PROC_showText( text$ )



DEFPROC_showText( this$ )
     LOCAL parts%
     LOCAL a$()
     LOCAL i%
     DIM a$(1)

     parts% = FN_split(this$, "//", a$() )

     FOR i% = 0 TO parts%-1
       PRINT STR$(i%)": "a$(i%)

DEFFN_readTempFile( filename$, RETURN message$ )
     REM reads the temporary file and returns the text
     REM as a string
ON ERROR LOCAL message$ = "Cannot read from temp file." : = ""
     LOCAL file%, temp$
     file% = OPENIN( @tmp$+filename$ )
     WHILE NOT(EOF#file%)
       INPUT#file%, temp$
       text$ += (temp$+CHR$(13)+CHR$(10))
     = text$

     DEFFN_deleteTempFile( filename$, RETURN message$ )
     ON ERROR LOCAL message$ = "Cannot delete temp file": = FALSE
"DEL "+@tmp$+filename$
     = TRUE