Skip to content

File

Spice
import "std/io/file";

File struct

A handle to an open file, wrapping the underlying C file pointer and tracking the end-of-file state

Methods

close

Spice
public f<bool> File.close()

Closes the file behind the provided file pointer.

Returns: bool — True if successful, false if not

getCursorPos

Spice
public inline f<unsigned long> File.getCursorPos()

Retrieves the current position of the file pointer.

Returns: unsigned long — Current position in the file in bytes

setCursorPos

Spice
public inline p File.setCursorPos(long offset, int origin = SEEK_SET)

Sets the position of the file pointer to a specific position.

Parameters

Name Type Description
offset long Position in the file in bytes
origin int Origin from which to set the position, can be SEEK_SET, SEEK_CUR, or SEEK_END (default: SEEK_SET)

readChar

Spice
public f<char> File.readChar()

Reads a singe char from the file.

Returns: char — Char in form of an int, because of EOF = -1.

readLine

Spice
public f<String> File.readLine()

Reads a single line from the file.

Returns: String — Line in form of a string

write

Spice
public f<bool> File.write(char value)

Writes a single character to the file.

Parameters

Name Type Description
value char Character to write

Returns: bool — True if successful, false if not

write

Spice
public f<bool> File.write(string value)

Writes a string to the file.

Parameters

Name Type Description
value string String to write

Returns: bool — True if successful, false if not

write

Spice
public f<bool> File.write(const String& value)

Writes a string to the file.

Parameters

Name Type Description
value const String& String to write

Returns: bool — True if successful, false if not

getSize

Spice
public f<unsigned long> File.getSize()

Returns the size of the file in bytes.

Returns: unsigned long — File size in bytes

isEOF

Spice
public inline f<bool> File.isEOF()

Checks if the end of the file is reached.

Returns: bool — EOF reached / not reached

getRawHandle

Spice
public const f<FilePtr> File.getRawHandle()

Get raw file handle

Returns: FilePtr — File handle

Functions

createFile

Spice
public f<bool> createFile(string path)

Creates an empty file on disk similar to the 'touch' command on Linux.

Parameters

Name Type Description
path string Path to the file

Returns: bool — True if successful, false if not

changeFilePermissions

Spice
public f<bool> changeFilePermissions(string path, short mode)

Changes the permissions of a file.

Parameters

Name Type Description
path string Path to the file
mode short New permissions

Returns: bool — True if successful, false if not

deleteFile

Spice
public f<bool> deleteFile(string path)

Deletes a file from disk.

Parameters

Name Type Description
path string Path to the file

Returns: bool — True if successful, false if not

openFile

Spice
public f<Result<File>> openFile(string path, string mode = MODE_READ)

Opens a (new) file at the specified path with the specified mode.

There are predefined constants for the mode available:
MODE_READ, MODE_WRITE, MODE_APPEND,
MODE_READ_WRITE, MODE_READ_WRITE_OVERWRITE, MODE_READ_WRITE_APPEND

Parameters

Name Type Description
path string Path to the file
mode string Mode to open the file in (default: MODE_READ)

Returns: Result<File> — File pointer

openFile

Spice
public f<Result<File>> openFile(String path, string mode = MODE_READ)

Opens the file at the given path in the given mode.

Parameters

Name Type Description
path String Path to the file
mode string Mode to open the file in (e.g. "r", "w", "a") (default: MODE_READ)

Returns: Result<File> — Result holding the opened file, or an error if the file could not be opened

readFile

Spice
public f<Result<String>> readFile(string path)

Reads a whole file from a given path.

Parameters

Name Type Description
path string Path to the file

Returns: Result<String> — Content in form of a string

writeFile

Spice
public f<Result<bool>> writeFile(string path, string content)

Writes a string to a file at a given path.

Parameters

Name Type Description
path string Path to the file
content string Content to write

Returns: Result<bool> — True if successful, false if not

getFileSize

Spice
public f<Result<unsigned long>> getFileSize(string path)

Returns the size of the file at the given paths in bytes.

Parameters

Name Type Description
path string Path to the file

Returns: Result<unsigned long> — File size in bytes

fileExists

Spice
public f<bool> fileExists(string path)

Checks if a file exists. The function also returns true if the specified path points to a directory.

Parameters

Name Type Description
path string Path to the file

Returns: bool — Existing / not existing

isFileReadable

Spice
public f<bool> isFileReadable(string path)

Checks if the read permissions to a file are given.

Parameters

Name Type Description
path string Path to the file

Returns: bool — Readable / not readable

isFileWritable

Spice
public f<bool> isFileWritable(string path)

Checks if the write permissions to a file are given.

Parameters

Name Type Description
path string Path to the file

Returns: bool — Writable / not writable

isFileExecutable

Spice
public f<bool> isFileExecutable(string path)

Checks if the execute permissions to a file are given.

Parameters

Name Type Description
path string Path to the file

Returns: bool — Executable / not executable

FilePtr alias

Alias for byte*.