File Input/Output Primer part 1

While it is also possible to do file I/O using COM, this primer will only cover native KiX functions.

There are a couple of ways to get input from a text file.  The methodology will vary, depending on what format the text is in.  Two of the methods are as follows:

ReadLine()
Action: Reads a line from a file.

Syntax: READLINE (file number)

Parameters: File number - A numeric expression indicating the file number of the file to open..  Possible values range from 1 to 10.

Remarks: READLINE reads a string ending in a carriage return.  If successful, the function returns the string without a carriage return.  If it encounters an error, @ERROR returns an error code.

Returns:
 -4 File not open for reading

 -3 File number not open

 -2 Invalid file number specified

 -1 End of file

  0 Line read successfully

See Also: Close(), Open(), WriteLine()

Example:
IF Open(3, @LDRIVE + "\CONFIG\SETTINGS.INI") = 0
  $x = ReadLine(3)
  WHILE @ERROR = 0
    ? "Line read: [" + $x + "]"
    $x = ReadLine(3)
  LOOP
  Close (3)
ELSE
  BEEP
  ? "Config file not opened, error code: [" + @ERROR + "]"
ENDIF

Before ReadLine() or WriteLine() can be used, the file need first be opened.  You can open a file for input or output, but not both.  Afterwards, it must be closed.  A file open for read by one user cannot be opened for write by another.  The opposite is also true.  This makes for a cumbersome methodology for multi-user access as extensive error checking and timeout loops would need be deployed.

------

ReadProfileString()
Action: Retrieves a string from an initialization file.

Syntax: READPROFILESTRING ("file name", "section", "key")

Parameters:
File name - A string that names the initialization file.  If this parameter does not include a full path, Windows searches for the file in the Windows directory.

Section - A string that specifies the section containing the key name.  If this parameter is empty, READPROFILESTRING returns all section names in the file.

Key - A string containing the key name whose associated string is to be retrieved.  If this parameter is empty, all key names in the section specified by section are returned.


Remarks: This function is provided for compatibility with 16-bit Windows – based applications.  Win32 – based applications store initialization information in the registry.

Returns: Function returns a string representing the value of the specified key.

Example:
$dev = ReadProfileString("win.ini", "Windows", "Device")
If @ERROR = 0
  ? "Windows device = " + $dev
Endif

Unlike ReadLine, ReadProfileString is much less susceptable to contention in a multi-user environment since the open and close are handled within the API.  It is limited however, to reading INI-style formatted data.

Next time I will cover output methods.

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.