cheatah
Class

io::File

A Python-like file object over std::fstream.

RAII closes on scope exit — the C++ analog of with open(...) as f:. Move-only (copying a file handle is meaningless), like Python file objects.

Functions

fn File · 4 overloads
File()=defaultsource#
File(const std::string &path, std::string_view mode)source#
File(const File &)=deletesource#
File(File &&)=defaultsource#

Construct a closed file (no stream attached).

Leaves the underlying stream default-constructed and unopened, so is_open() is false until a later open(); read/write calls on it are no-ops that fail silently.

Complexity

O(1).

Allocation

none.

System testStdlibE2E.Io
fn operator= · 2 overloads
File & operator=(const File &)=deletesource#
File & operator=(File &&)=defaultsource#

Move-assign, taking over the other handle (the moved-from File becomes closed).

Returns

reference to this File.

fn ~File() source#

Close the stream if still open.

Complexity

O(1).

Allocation

none.

System testStdlibE2E.Io
fn void open(const std::string &path, std::string_view mode) source#

(Re)open path in mode.

Opens the stream on path; it does not first close an already-open handle, so reuse this on a closed File. Mode follows Python: r read, w truncate-write, a append, + adds the opposite direction, b binary; an unrecognized/empty mode defaults to r.

Parameters
path

filesystem path.

mode

Python-style mode string.

Complexity

O(1) plus the OS open.

Allocation

none.

System testStdlibE2E.Io
fn bool is_open() const source#

Is the underlying stream open?

Returns

true iff a file is attached and open.

Complexity

O(1).

Allocation

none.

System testStdlibE2E.Io
fn void close() source#

Close the underlying stream (no-op if already closed).

Complexity

O(1).

Allocation

none.

System testStdlibE2E.Io
fn std::string read() source#

Read the whole remaining file.

Drains the stream buffer from the current position to EOF in one shot, so a prior readline()/read() returns only what is left; returns "" at EOF or on a closed file.

Returns

the remaining bytes as one string.

Complexity

O(n) in bytes read.

Allocation

allocates the returned string (buffered through a stringstream).

System testStdlibE2E.Io
fn std::string readline() source#

Read the next line.

Consumes through the next \n (which is discarded). Because both a genuine empty line and EOF yield "", the return value alone cannot distinguish them — check is_open()/EOF separately if that matters.

Returns

the line with its newline stripped; "" at EOF.

Complexity

O(line length).

Allocation

allocates the returned string.

Compile-run testIoCompileRun.Readline
System testStdlibE2E.Io
fn std::vector< std::string > readlines() source#

Read all remaining lines.

Repeatedly getlines from the current position until EOF, pushing each newline-stripped line; returns an empty vector at EOF, and a trailing newline does not produce a final empty element.

Returns

a vector of lines (newlines stripped).

Complexity

O(n) in bytes.

Allocation

allocates the vector and each line string.

Compile-run testIoCompileRun.Readlines
System testStdlibE2E.Io
fn void write(const T &value) source#

Write a streamable value to the file.

Streams value through operator<< exactly as written — no separator and no trailing newline are added (unlike print()), so the caller supplies any \n; data may stay buffered until the stream is flushed or the File is closed.

Parameters
value

any streamable value.

Complexity

O(output length).

Allocation

none (writes straight to the stream buffer).

Warning

A failed write is not reported: on a closed File or a failed stream the data is silently dropped (only the stream's error state records it) — there is no exception and no return value.

System testStdlibE2E.Io
fn static std::ios::openmode translate_mode(std::string_view mode) source#

Map a Python mode string (r/w/a, optional +/b) to std::ios::openmode.

Constants & variables

var std::fstream stream_ source#