File Input/Output Primer part 2
There are at least three ways of sending output to a text file. The methodology will vary, depending on what the source of the text is. Also, the format of the file and its intended use will determine the best method. The methods are as follows:
RedirectOutput()
Action: Allows you to redirect all screen output to a file.
Syntax: REDIRECTOUTPUT ("file name", overwrite)
Parameters:
File name - A string that names the file to which to redirect the output. If this parameter is an empty string (""), output is redirected to the screen.
Overwrite (optional) - Optional numeric value indicating whether to clear the output file before writing any data to it. This parameter can have the following values:
0 New output data appended to the existing contents of file.
1 All data in file overwritten when the output is redirected to the file.
Returns:
0 Output redirected
Error code Function failed
Example:
IF RedirectOutput("logon.log") = 0
? "Opened 'logon.log' at " + @TIME ?
$rc = RedirectOutput("")
ENDIF
RedirectOutput() requires less code than WriteLine(). Anything that would otherwise output to the console simply redirects to the file specified. It is just as prone to contention as Open()/ReadLine()/WriteLine().
------
WriteLine()
Action: Appends a line to the end of the file indicated by File Number. If WriteLine encounters an error, @ERROR is set to the relevant error code.
Syntax: WRITELINE (file number, “linetowrite”)
Parameters:
File number - A numeric expression indicating the file number of the file to open. Possible values range from 1 to 10.
LineToWrite - The string you want to write to the file.
Remarks: WriteLine does not automatically append a , so if you want to write a , you should add it to the string (as in : $LineToWrite + Chr(13) + Chr(10)).
Returns:
-4 File not open for writing
-3 File number not open
-2 Invalid file number specified
-1 End of file
0 Line written successfully
See Also: Close( ), Open( ), ReadLine( )
Example:
IF Open( 3 , "C:\TEMP\LOG.TXT" , 5 ) = 0
$x = WriteLine( 3 , "KiXtart started at " + @TIME + @CRLF )
ELSE
BEEP
? "failed to open file, error code : [" + @ERROR + "]"
ENDIF
The @CRLF macro is new to KiX 4. Prior versions can create a $CRLF variable to use instead.
$CRLF = Chr(13) + Chr(10)
All the above methods are prone to contention, making for a cumbersome methodology for multi-user access as extensive error checking and timeout loops would need be deployed.
Next time I will cover WriteProfileString()
Note: Portions of this were taken from the HTML help file created and compiled by ScriptLogic Corporation.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.