File I/O Library
1.12.0¶
#include <std/file.pat>
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::Handle
1.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 to a file on disk. Either absolute or relative to current working directory |
|
A value of |
|
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 to the opened file |
std::file::read(std::file::Handle handle, u128 size) -> str
1.12.0¶
Reads data as a string from a file
Similar to C’s fread
Parameter |
Description |
---|---|
|
Handle to the opened file |
|
Number of characters to read. If value is |
|
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 to the opened file |
|
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 to the opened file |
|
Offset in the file to seek to |
std::file::size(std::file::Handle handle) -> u128
1.12.0¶
Queries the length of a file
Parameter |
Description |
---|---|
|
Handle to the opened file |
|
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 to the opened file |
|
New size of file. If |
std::file::flush(std::file::Handle handle)
1.12.0¶
Flushes all currently pending disk operations
Similar to C’s fflush
Parameter |
Description |
---|---|
|
Handle to the opened file |
std::file::remove(std::file::Handle handle)
1.12.0¶
Deletes a file from disk
Parameter |
Description |
---|---|
|
Handle to the opened file |