Wheel IDE

Online demo »

File

This file contains a procedures to handle files.

Constants

The file module.
Source: lib/modules/file.whl, line: 9
MODULE_FILE8

Module calls to handle files.
Source: lib/modules/file.whl, line: 12
FILE_EXISTS0
FILE_OPEN_READ1
FILE_OPEN_WRITE2
FILE_READ_NUMBER3
FILE_READ_STRING4
FILE_WRITE_NUMBER5
FILE_WRITE_STRING6
FILE_CLOSE7
FILE_DELETE8
FILE_SIZE9

Procedures

fileExists
Source: lib/modules/file.whl, line: 24

Check if a file exists

proc fileExists(string filename)
Parameters:
NameTypeDescription
filenamestringThe filename.
Return: 1 If the file exists, 0 if not.


fileOpenRead
Source: lib/modules/file.whl, line: 32

Open a file for reading.

proc fileOpenRead(string filename)
Parameters:
NameTypeDescription
filenamestringThe filename.

fileOpenWrite
Source: lib/modules/file.whl, line: 39

Open a file for writing.

proc fileOpenWrite(string filename)
Parameters:
NameTypeDescription
filenamestringThe filename.

fileReadNumber
Source: lib/modules/file.whl, line: 46

Read a number from a file.

proc fileReadNumber(number handle)
Parameters:
NameTypeDescription
handlenumberThe handle of the file, the file should be opened first with the fileOpen call.

The simulator contains a `test.rtf` file with the following values:
45, 99, "Hello world", 15

#include "lib/modules/standard.whl"
#include "lib/modules/file.whl"
#include "lib/modules/screen.whl"

proc main()
    ; Open the file:
    number handle
    string filename = "test.rtf"
    handle = fileOpen(@filename)

    ; Read two numbers form the opened file...
    drawNumber(10, 10, fileReadNumber(handle))
    drawNumber(10, 30, fileReadNumber(handle))

    ; Read a string from the opened file:
    string s
    fileReadString(handle, @s)
    drawText(10, 50, s)

    ; Read the final number:
    drawNumber(10, 70, fileReadNumber(handle))

    halt()
end

fileReadString
Source: lib/modules/file.whl, line: 53

Read a string from a file.

proc fileReadString(number handle, string result)
Parameters:
NameTypeDescription
handlenumberThe handle of the file, the file should be opened first with the fileOpen call.
resultstringThe string which is read from the file.

fileWriteNumber
Source: lib/modules/file.whl, line: 61

Write a number to a file.

proc fileWriteNumber(number handle, number n)
Parameters:
NameTypeDescription
handlenumberThe handle of the file, the file should be opened first with the fileOpen call.
nnumberThe number to write to the file.

This demo shows how to write values to a file and read them back.

#include "lib/modules/standard.whl"
#include "lib/modules/file.whl"
#include "lib/modules/screen.whl"

proc main()
    ; Open the file:
    number handle
    string filename = "new.rtf"
    handle = fileOpen(@filename)

    ; Write two numbers to the file:
    fileWriteNumber(handle, 1546)
    fileWriteNumber(handle, 94)

    ; Close the file...
    fileClose(handle)
    handle = fileOpen(@filename)

    ; Read two numbers form the opened file...
    drawNumber(10, 10, fileReadNumber(handle))
    drawNumber(10, 30, fileReadNumber(handle))

    ; And delete the file...
    fileDelete(handle)

    halt()
end

fileWriteString
Source: lib/modules/file.whl, line: 69

Write a string to a file.

proc fileWriteString(number handle, string s)
Parameters:
NameTypeDescription
handlenumberThe handle of the file, the file should be opened first with the fileOpen call.
sstringThe string to write to the file.

This demo shows how to write values to a file and read them back.

#include "lib/modules/standard.whl"
#include "lib/modules/file.whl"
#include "lib/modules/screen.whl"

proc main()
    ; Open the file:
    number handle
    string filename = "new.rtf"
    handle = fileOpen(@filename)

    ; Write two numbers to the file:
    fileWriteNumber(handle, 1546)
    fileWriteNumber(handle, 94)

    ; Close the file...
    fileClose(handle)
    handle = fileOpen(@filename)

    ; Read two numbers form the opened file...
    drawNumber(10, 10, fileReadNumber(handle))
    drawNumber(10, 30, fileReadNumber(handle))

    ; And delete the file...
    fileDelete(handle)

    halt()
end

fileClose
Source: lib/modules/file.whl, line: 77

Close a file.

proc fileClose(number handle)
Parameters:
NameTypeDescription
handlenumberThe handle of the file, the file should be opened first with the fileOpen call.

fileDelete
Source: lib/modules/file.whl, line: 84

Delete a file.

proc fileDelete(string filename)
Parameters:
NameTypeDescription
filenamestringThe filename to delete.

fileSize
Source: lib/modules/file.whl, line: 91

Get the size of a file.

proc fileSize(string filename)
Parameters:
NameTypeDescription
filenamestringThe filename.
Return: The file size in bytes.