Page 1 of 1 1
Topic Options
#81930 - 2002-03-15 03:06 AM File Input/Output Primer
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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.

Top
#81931 - 2002-03-16 05:01 AM Re: File Input/Output Primer
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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.

Top
#81932 - 2002-03-23 05:55 AM Re: File Input/Output Primer
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
File Input/Output Primer part 3

WriteProfileString()
Action: Copies a string to an initialization file.

Syntax: WRITEPROFILESTRING ("file name", "section", "key", "string")

Parameters:
File name - String identifying the initialization file.

Section - String containing the name of the section of the initialization file where string is copied.  If the section does not exist, it is created.  The section name is not case-sensitive, and can contain any combination of uppercase and lowercase letters.

Key - String containing the name of the key to associate with string.  If the key does not exist in the specified section, it is created.  If this parameter is empty, the entire section, including all entries within the section, is deleted.

String - String to write to the file.  If this parameter is empty, the key identified by key is deleted.

Returns:
  0 Profile string written
  Error code Function failed

Example:

$Error ="Download of vpcur.lst failed!"
GoTo ExitError
...
$RC = WriteProfileString($WorkDir + "\PatternUpdater.ini","NAV","Updated",@Date + " - " + @Time)
$Error =""
:ExitError
$RC = WriteProfileString($WorkDir + "\PatternUpdater.ini","NAV","Error",$Error)
Exit 1


While INI files were primarily for 16-bit apps, they still exist today.  WriteProfileString() can be used not only to manipulate existing application INI files, but also to create your own to store all manner of data efficiently.  The APIs used for WriteProfileString() and ReadProfileString() include everything you may need to write of enumerate data in an organized fashion without duplication or the need to read in entire files and parse them line by line.  Also, since the API handles all the I/O, including opening and closing of the file, contention is pretty much eliminated.

Be aware that Windows 9x/ME has a 64k limit on INI file size.  It is therefor advisable to split up the data into multiple files.  Admin scripts can then be utilized to consolidate the data into CSV files, MDB or SQL tables.

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.

Top
Page 1 of 1 1


Moderator:  Jochen, Radimus, Glenn Barnas, Allen, Arend_, ShaneEP, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 559 anonymous users online.
Newest Members
min_seow, Audio, Hoschi, Comet, rrosell
17881 Registered Users

Generated in 0.049 seconds in which 0.023 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org