Back to index

File Input/Output:

Function MainDir as String

Returns path where currently executed .bas file is located. Can be very useful if script uses some external resourses (images, etc) that are located near executable script.

Example:

     pic = LoadPicture(MainDir+"mybmp.bmp")

Function FileExists(filename) as Integer

Checks if the specified file exists.

Where filename - File name and path to check
Returns 1 if the file exists, otherwise 0.

Example:

     fn = "c:\\MyDocs\\tmpfile.dat"
     if FileExists(hn) then
       handle = OpenFile fn
     else
       handle = CreateFile fn
     end if

Function PathExists(path) as Integer

Tests whether a folder exists.
The folder is specified in a path. The path can omit the drive letter, in which case the drive letter is taken from the session path.
If the path is badly formed, for instance if it contains illegal characters, or any directory name consists of a single or double dot, or any directory name includes wildcard characters, the function returns 0 (false).
A filename may be specified in the path, and is ignored, but note that if a filename is included in the path, its name must not contain any illegal characters - this is considered a badly formed path and causes the function to return false. If no filename is specified, the path should end in a backslash.

Where path - A path specifying the folder to test for.
Returns 1 if the folder specified in path exists, 0 (false) if not. False is also returned if the specified path is badly formed.

Sub EnsurePathExists(path)

Makes one or more directories, if they do not already exist.

Any valid path component in the specified path that does not already exist is created as a directory. If the specified path already exists, the function returns normally.
A filename may be specified in the path, and is ignored, but note that if a filename is included in the path, its name must not contain any illegal characters - this is considered a badly formed path and causes the function to return false. If no filename is specified, the path should end in a backslash.

Where path - Path to ensure exists

Function CreateFile(filename) as Integer

Creates and opens a new file.
If the resulting file exists, then no file is created and opened and return value is -1. Note that the resulting path must exist for this function to succeed.

Where filename - The name of the file. Any path components which are not specified here are taken from the session path. Note that the resulting path must exist for this function to succeed.

Returns a handle to opened file if file creation was successful, otherwise returns -1. File handle value is used in all file operations, like WriteFile, ReadFile, ClosedFile etc

Function OpenFile(filename) as Integer

Opens an existing file for reading or writing. If the file does not already exist, then -1 is returned .

Where filename - The name of the file.

Returns a handle to opened file if file opening was successful, otherwise returns -1. File handle value is used in all file operations, like WriteFile, ReadFile, ClosedFile etc

Sub CloseFile(handle)

Closes the file specified by handle. When closing a file that you have written to it also flushes internal buffers.

Where handle - a value that has been retrieved from OpenFile/CreateFile function and specifies a handle to opened file.

Sub DeleteFile(filename)

Deletes specified file.

Where filename - The name of the file.

Sub WriteFile(handle, value)

Writes to file.

Note: WriteFile always appends infomation to file. See example on the bottom of this page that demonstrates WriteFile.

Where handle - a value that has been retrieved from OpenFile/CreateFile function and specifies a handle to opened file.
value - can be any variable of any type, epxression, - any value sBasic operates with.

Function ReadFile(handle) as AnyType

Reads from file.
When an attempt is made to read beyond the end of the file, no error is returned. The returned value type will be integer and value equal to 0.
See example on the bottom of this page that demonstrates ReadFile function.

Where handle - a value that has been retrieved from OpenFile/CreateFile function and specifies a handle to opened file.
Returns a value read from file. Returned value type can be any type.

Sub RewindFile(handle)

Rewind file and set current file point to beging that next read operation will get first value located in file.

Where handle - a value that has been retrieved from OpenFile/CreateFile function and specifies a handle to opened file.

Function IsEof(handle) as Integer

Checks if end of file reached

Where handle - a value that has been retrieved from OpenFile/CreateFile function and specifies a handle to opened file.

Returns 1 if end of file, 0 - if it is not

Example:

  FileName = MainDir+"data.dat"
  If FileExists(FileName) Then
    DeleteFile FileName
  End if
  
  file = CreateFile FileName

  if file = -1 then
    MessageBox "Error creating file!"
    Stop
  endif

  ' Let us write something to our file
  WriteFile file, 10 ' some integer const
  WriteFile file, 3.14 ' some floating const
  WriteFile file, "Hello world" ' some string const

  ' Now we are going to read information from file
  ' We need to rewind file first cuz WriteFile always
  ' appends information to file and current file pointer is
  ' set to End of file

  RewindFile

  ' Let us read all value that are in file
  ' Read from begin of file till end of file
  While Not IsEof(file) 
    Print ReadFile(file) ' reads and prints a value read from file
  While end

  CloseFile file



Copyright (c) 2000-2006 by Smartphoneware. All rights reserved.