Mind gone blank on file writing

Previous topic - Next topic

Gary

Sorry if this is a simple question but cant see anything obvious in the help file

I want to create a couple of log files for a program Im writing. Basically all it will be is a series of numbers which I will then throw into a spreadsheet and create a graph from.

Is there a really simple way to open a file, append a line to the bottom and close it? There will be 10000 entries in the log files so cant use PUTFILE which would be ideal if it was not linked to an CHAR length

Cheers
Gary

Kitty Hello

openfile "test", -1 // -1 = append?
WRITELINE...
CLOSEFILE

Gary

Cheers Kitty, I was looking in the online manual at the input/output section and other file commands are listed but not those 2

Slydog

#3
Here's a 'log' function that may be useful (never tested):
- 'file$' allows you to override the default file.
- 'include_time%' is set to FALSE in this example, since by your question you wont need it. If you want all / most of your log entries to include a timestamp, set this to TRUE in the function declaration.

Code (glbasic) Select
FUNCTION Log: message$, file$="", include_time%=FALSE
    LOCAL fh% ' File Handle
    IF file$ = "" THEN file$ = "log.txt"  // If no file specified, use default log file
    fh = GENFILE()
    IF NOT OPENFILE(fh, file$, -1)
        DEBUG "Can't Open Log File: [" + file$ + "]\n"
        RETURN
    ENDIF
    IF include_time THEN message$ = PLATFORMINFO$("TIME") + ": " + message$
    WRITELINE fh, message$
    CLOSEFILE fh
ENDFUNCTION


Usage:
Code (glbasic) Select
Log("using defaults")  // Writes using default file and default time stamp setting (FALSE in this example)
Log("special file", "error.log") // Writes to a different file, with default time stamp setting
Log("time stamped", "", TRUE) // Writes to default file with a time stamp
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]