# File I/O Library1.12.0¶

#include <std/file.pat>

This namespace contains various pointer helper functions

Warning

These functions are considered dangerous and require the user to confirm that they really want to run this pattern.

## Types¶

### Handle¶

A type representing a File Handle

Mode

File Opening Mode - Read: Open a file in read-only mode. Throws an error if the file doesn’t exist. - Write: Open a file in Read/Write mode. Throws an error if the file doesn’t exist. - Create: Opens a new file in Read/Write mode. If the file doesn’t exist, it will be created. If it does exist, it will be cleared.

## Functions¶

### std::file::open(str path, std::file::Mode mode) -> std::file::Handle1.12.0¶

Opens a file on disk and returns a handle to it Similar to C’s fopen

Important

Every file opened MUST be closed before the program ends or the handle goes out of scope. Otherwise File handles will be leaked and the file cannot be opened again until the runtime is restarted.

Parameter

Description

path

Path to a file on disk. Either absolute or relative to current working directory

mode

A value of std::file::Mode specifying how to open the file

return

Handle to the opened file

### std::file::close(std::file::Handle handle)1.12.0¶

Closes a file previously opened with std::file::open() Similar to C’s fclose

Parameter

Description

handle

Handle to the opened file

### std::file::read(std::file::Handle handle, u128 size) -> str1.12.0¶

Reads data as a string from a file Similar to C’s fread

Parameter

Description

handle

Handle to the opened file

size

Number of characters to read. If value is 0, the entire file is read.

return

String containing read characters

### std::file::write(std::file::Handle handle, str data)1.12.0¶

Writes data in form of a string to a file Similar to C’s fwrite

Parameter

Description

handle

Handle to the opened file

data

Data to write to current offset in file

### std::file::seek(std::file::Handle handle, u128 offset)1.12.0¶

Seeks to a specific offset in the file Similar to C’s fseek

Parameter

Description

handle

Handle to the opened file

offset

Offset in the file to seek to

### std::file::size(std::file::Handle handle) -> u1281.12.0¶

Queries the length of a file

Parameter

Description

handle

Handle to the opened file

return

Size of file in bytes

### std::file::resize(std::file::Handle handle, u128 size)1.12.0¶

Resizes a file Similar to C’s ftruncate

Parameter

Description

handle

Handle to the opened file

size

New size of file. If size is smaller than the current size, excess characters will be deleted

### std::file::flush(std::file::Handle handle)1.12.0¶

Flushes all currently pending disk operations Similar to C’s fflush

Parameter

Description

handle

Handle to the opened file

### std::file::remove(std::file::Handle handle)1.12.0¶

Deletes a file from disk

Parameter

Description

handle

Handle to the opened file