Showing posts with label temporary files. Show all posts
Showing posts with label temporary files. Show all posts

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$ + "//" )
     ENDWHILE


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


     
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
     LOCAL
file%
     file% = OPENOUT( @tmp$+filename$ )
     PRINT#file%, text$
     CLOSE#file%
     = 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
         PRINT
"<ERROR>"'errmessage$
       ELSE
         PRINT
"Temp file deleted"
         PRINT "file contains:"
         PROC_showText( text$ )

       ENDIF
     ELSE
       PRINT
"<ERROR>"'errmessage$
     ENDIF

     STOP


     
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%)
     NEXT
     ENDPROC


     
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))
     ENDWHILE
     CLOSE
#file%
     = text$


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




Reading and writing temporary files 01

Just because I was asked this question today:
How do you read and write to temporary files in BBC BASIC for Windows?

We shall create one temporary file in program one, which will write a short message and the current time into a text file.  Program two will read the text from this file and display on the screen.

Code for Program one:

     REM this program writes a
     REM temporary text file

     
text1$ = "Hello, this file was created by program 1 @"
     text2$ = TIME$

     REM create the file
     
PRINT "writing temp file"
     PROC_createTempFile( "mytemp.tmp", text1$+text2$ )
     PRINT "done"
     STOP


     
DEFPROC_createTempFile( filename$, text$ )
     REM writes the text into the temp file whose
     REM filename is passed in filename$
     
LOCAL file%
     file% = OPENOUT( @tmp$+filename$ )
     PRINT#file%, text$
     CLOSE#file%
     ENDPROC






Code for Program two:

     REM this program reads from a
     REM temporary text file

     
PRINT "looking for the text file."
     text$ = FN_readTempFile( "mytemp.tmp" )
     PRINT "file contains:"
     PRINT text$
     STOP


     
DEFFN_readTempFile( filename$ )
     REM reads the temporary file and returns the text
     REM as a string
     
LOCAL file%, temp$
     file% = OPENIN( @tmp$+filename$ )
     INPUT#file%, temp$
     CLOSE#file%
     = temp$



If you need to navigate through Windows file explorer to find the files yourself then you should find that these programs are reading and writing to a folder located at %USERPROFILE%\AppData\Local\Temp

It would certainly be an advantage to delete the file after it has been used.  Also, it would be better if we could deal with unexpected behaviour and variable sized data lengths.

I will look at some improvements to this code in the next post.


Label

World Karma Game