std::fs::file
File Type
RAII wrapper around a POSIX file descriptor.
Fs::File owns a file descriptor and automatically closes it when dropped.
Use this instead of raw open/close calls for automatic resource cleanup.
Construction
| Method | Description |
|---|---|
File::open(path) |
Open existing file for reading |
File::create(path) |
Create/truncate file for writing |
File::openWithFlags(path, flags, mode) |
Full control over open flags |
Example
import Fs from "std::fs";
import Io from "std::io";
function main(): i32 {
let result: Result<Fs::File, Io::IoError> = Fs::File::open("/etc/hosts");
if (result.isError()) {
return 1;
}
let mut file: Fs::File = result.unwrap();
let meta: Result<Fs::Metadata, Io::IoError> = file.metadata();
return 0;
}
namespace FsFile-descriptor backed filesystem types.
Records
record FileRAII wrapper around an owned POSIX file descriptor.
Members
fd(&self): i32Returns the raw file descriptor.
static create(path: str): Result<File, IoError>Creates or truncates a file for writing.
static open(path: str): Result<File, IoError>Opens an existing file for reading.
static openWithFlags(path: str, flags: i32, mode: u32): Result<File, IoError>Opens a file with full control over flags and mode.
drop(&mut self): voidCloses the file descriptor.
fd(&self): i32Returns the raw file descriptor.
metadata(&self): Result<Metadata, IoError>Returns file metadata via fstat.
read(&mut self, buf: *mut void, count: u64): Result<i64, IoError>Reads up to count bytes into buf. Returns 0 on EOF.
seek(&mut self, offset: i64, whence: i32): Result<i64, IoError>Repositions the file offset.
write(&mut self, buf: *void, count: u64): Result<i64, IoError>Writes up to count bytes from buf.