Non‐refcounted RAII wrapper for FILE*

Synopsis

Declared in <streams.h>

class AutoFile;

Description

Will automatically close the file when it goes out of scope if not null. If you're returning the file pointer, return file.release(). If you need to close the file early, use autofile.fclose() instead of fclose(underlying_FILE).

Note

If the file has been written to, then the caller must close it explicitly with the fclose() method, check if it returns an error and treat such an error as if the write() method failed. The OS's fclose(3) may fail to flush to disk data that has been previously written, rendering the file corrupt.

Member Functions

Name

Description

AutoFile [constructor]

Constructors

~AutoFile [destructor]

Close the wrapped file if still open, logging any close error.

operator= [deleted]

Deleted copy assignment; AutoFile is non‐copyable.

Commit

Wrapper around FileCommit().

IsNull

Return true if the wrapped FILE* is nullptr, false otherwise.

SetObfuscation

Continue with a different XOR key

Truncate

Wrapper around TruncateFile().

detail_fread

Implementation detail, only used internally.

fclose

Close the wrapped file early and return the C fclose result.

feof

Return true if the end‐of‐file indicator is set on the wrapped file.

ignore

Skip nSize bytes from the file.

operator>>

Deserialize an object from the file and return this wrapper.

read

Read raw bytes from the file into dst, applying deobfuscation.

release

Get wrapped FILE* with transfer of ownership.

seek

Wrapper around fseek(). Will throw if seeking is not possible.

size

Return the size of the file. Will throw if unknown.

tell

Find position within the file. Will throw if unknown.

write

Write raw bytes from src to the file, applying obfuscation.

write_buffer

Write a mutable buffer more efficiently than write(), obfuscating the buffer in‐place.

operator<<

Serialize an object to the file and return this wrapper.

Protected Data Members

Name

Description

m_file

Wrapped C file handle, or nullptr once released or closed.

m_obfuscation

XOR key applied to data as it is read or written.

m_position

Cached current file position, if known.

m_was_written

True once any data has been written through this wrapper.

Created with MrDocs