CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Native Files

Data Structures

struct  File
 

Macros

#define fileClose(pfile)   (unused_noeval(&((*(pfile))->_is_File)), _fileClose((File**)(pfile)))
 
#define fileWriteString(self, str, byteswritten)   File_writeString(File(self), str, byteswritten)
 
#define fileRead(self, buf, sz, bytesread)   ((self) ? ((self)->_->read(File(self), buf, sz, bytesread)) : false)
 
#define fileWrite(self, buf, sz, byteswritten)   ((self) ? ((self)->_->write(File(self), buf, sz, byteswritten)) : false)
 
#define fileTell(self)   ((self) ? ((self)->_->tell(File(self))) : -1)
 
#define fileSeek(self, off, seektype)   ((self) ? ((self)->_->seek(File(self), off, seektype)) : -1)
 
#define fileFlush(self)   ((self) ? ((self)->_->flush(File(self))) : false)
 

Typedefs

typedef File FSFile
 A file opened on the OS filesystem with fsOpen()
 
typedef enum FSSeekTypeEnum FSSeekType
 
typedef struct File File
 

Enumerations

enum  FSOpenFlags {
  FS_Read = 1 , FS_Write = 2 , FS_Create = 4 , FS_Truncate = 8 ,
  FS_Lock = 16 , FS_Overwrite = (FS_Write | FS_Create | FS_Truncate)
}
 
enum  FSSeekTypeEnum { FS_Set = 0x00010000 , FS_Cur = 0x00020000 , FS_End = 0x00030000 }
 

Functions

FSFilefsOpen (strref path, flags_t flags)
 
bool fsRead (FSFile *file, void *buf, size_t sz, size_t *bytesread)
 
bool fsWrite (FSFile *file, const void *buf, size_t sz, size_t *byteswritten)
 
bool fsWriteString (FSFile *file, strref str, size_t *byteswritten)
 
int64 fsTell (FSFile *file)
 
int64 fsSeek (FSFile *file, int64 off, FSSeekType seektype)
 
bool fsFlush (FSFile *file)
 

Detailed Description

Provides direct access to the operating system's file I/O APIs for synchronous read/write operations. This is a thin wrapper around platform-native file handles with minimal buffering.

Key characteristics:

An open file is a File object no matter which layer opened it, so FSFile and VFSFile are both just other names for File. A function that reads and writes can take a File* and be handed either one; the type does not record where the handle came from.

The fs* calls below and the vfs* calls in vfs.h are the same operations as fileRead(), fileWrite() and the rest, under names that say which layer the file was opened through. Which spelling you use is a matter of readability.

For higher-level filesystem operations (directories, metadata), see fs.h. For virtual filesystem abstraction, see vfs.h.

Macro Definition Documentation

◆ fileClose

#define fileClose (   pfile)    (unused_noeval(&((*(pfile))->_is_File)), _fileClose((File**)(pfile)))

bool fileClose(File **pfile);

Closes a file and releases your reference to it

Flushes pending writes, closes the file, and sets the pointer to NULL. Accepts any kind of File. If something else still holds a reference to the same File, that reference stays valid, but reads and writes through it fail.

Parameters
pfilePointer to the file handle (the handle may be NULL)
Returns
true if successful, false if an error occurred while flushing or closing, or if the handle was NULL. The reference is released either way.

Example:

File *f = fsOpen(_SL("data.bin"), FS_Overwrite);
fileWrite(f, data, sizeof(data), NULL);
if (!fileClose(&f))
return false;
FSFile * fsOpen(strref path, flags_t flags)
#define fileWrite(self, buf, sz, byteswritten)
Definition fileobj.h:129
#define fileClose(pfile)
Definition file.h:65
@ FS_Overwrite
Create or truncate for writing.
Definition fs.h:326
#define _SL(s)
Inline ASCII string literal with compile-time embedded length (STR_LEN8). Content must be < 200 bytes...
Definition strliteral.h:207
Definition fileobj.h:59

Definition at line 65 of file file.h.

◆ fileFlush

#define fileFlush (   self)    ((self) ? ((self)->_->flush(File(self))) : false)

bool fileFlush(File* self);

Flushes buffered writes to storage

Forces buffered data out to the storage device. This is slow, so only do it when durability matters right now; closing the file flushes it anyway.

Returns
true if successful, false on error

Definition at line 152 of file fileobj.h.

◆ fileRead

#define fileRead (   self,
  buf,
  sz,
  bytesread 
)    ((self) ? ((self)->_->read(File(self), buf, sz, bytesread)) : false)

bool fileRead(File* self, void* buf, size_t sz, size_t* bytesread);

Reads data from the file

Reads up to sz bytes from the current position into buf and advances the position by the number of bytes actually read. Reading less than requested is not an error; it means the end of the file was reached.

Parameters
bufBuffer to receive the data (must be at least sz bytes)
szMaximum number of bytes to read
bytesreadReceives the number of bytes read (0 at end of file)
Returns
true if the read succeeded, false on an I/O error (bytesread is set to 0)

Definition at line 116 of file fileobj.h.

◆ fileSeek

#define fileSeek (   self,
  off,
  seektype 
)    ((self) ? ((self)->_->seek(File(self), off, seektype)) : -1)

int64 fileSeek(File* self, int64 off, FSSeekType seektype);

Changes the current position in the file

Parameters
offOffset in bytes, which may be negative for FS_Cur and FS_End
seektypeWhere to measure from: FS_Set (start), FS_Cur (current), FS_End (end)
Returns
New position in bytes from the start of the file, -1 on error

Definition at line 143 of file fileobj.h.

◆ fileTell

#define fileTell (   self)    ((self) ? ((self)->_->tell(File(self))) : -1)

int64 fileTell(File* self);

Gets the current position in the file

Returns
Current position in bytes from the start of the file, -1 on error

Definition at line 135 of file fileobj.h.

◆ fileWrite

#define fileWrite (   self,
  buf,
  sz,
  byteswritten 
)    ((self) ? ((self)->_->write(File(self), buf, sz, byteswritten)) : false)

bool fileWrite(File* self, const void* buf, size_t sz, size_t* byteswritten);

Writes data to the file

Writes sz bytes from buf at the current position and advances the position by the number of bytes written. Most of the time everything is written or the call fails, but a short write is possible on some filesystems, so check byteswritten when it matters.

Parameters
bufBuffer containing the data to write
szNumber of bytes to write
byteswrittenReceives the number of bytes written, or NULL
Returns
true if the write succeeded, false on an I/O error

Definition at line 129 of file fileobj.h.

◆ fileWriteString

#define fileWriteString (   self,
  str,
  byteswritten 
)    File_writeString(File(self), str, byteswritten)

bool fileWriteString(File* self, strref str, size_t* byteswritten);

Writes the contents of a string to the file

The string is written as-is, with no line ending added.

Parameters
strString to write
byteswrittenReceives the number of bytes written, or NULL
Returns
true if the write succeeded, false on an I/O error

Definition at line 95 of file fileobj.h.

Typedef Documentation

◆ File

typedef struct File File

An open file

Every open file is a File, whichever layer opened it: fsOpen() hands back one backed by an OS file handle, and vfsOpen() one backed by a VFS provider. Code that only reads and writes can take a File* and work with either.

A File is reference counted. fileClose() closes the file and releases your reference; objRelease() only releases the reference, and closes the file if it was the last one.

A file handle is single-owner: one thread at a time, like a C FILE*. Two threads may use two handles on the same file, but they must not share one handle.

Definition at line 16 of file fileobj.h.

◆ FSSeekType

typedef enum FSSeekTypeEnum FSSeekType

File seek origin

Specifies the reference point for fileSeek operations.

Enumeration Type Documentation

◆ FSOpenFlags

File open flags

Flags controlling how a file is opened. Combine with bitwise OR. The FS_Overwrite flag is a convenience combination of common flags.

Enumerator
FS_Read 

Open for reading.

FS_Write 

Open for writing.

FS_Create 

Create file if it doesn't exist.

FS_Truncate 

Truncate file to zero length on open.

FS_Lock 

Request exclusive access (other processes can read but not write)

FS_Overwrite 

Create or truncate for writing.

Definition at line 320 of file fs.h.

◆ FSSeekTypeEnum

File seek origin

Specifies the reference point for fileSeek operations.

Enumerator
FS_Set 

Seek from beginning of file (absolute position)

FS_Cur 

Seek from current file position (relative)

FS_End 

Seek from end of file (usually negative offset)

Definition at line 332 of file fs.h.

Function Documentation

◆ fsFlush()

bool fsFlush ( FSFile file)
inline

Flushes buffered writes to disk

Forces any buffered write data to be physically written to the storage device. This ensures data durability but may be slow. The OS may buffer writes for performance; this function ensures they reach the disk.

Parameters
fileOpen file handle
Returns
true if successful, false if flush failed (I/O error, disk full, etc.)
Note
Closing a file automatically flushes, so explicit flushing is only needed for long-lived files or when durability is critical (e.g., after writing a transaction log entry).

Definition at line 244 of file file.h.

References fileFlush.

◆ fsOpen()

FSFile * fsOpen ( strref  path,
flags_t  flags 
)

Opens a file for I/O operations

Creates a file handle for reading, writing, or both. Close it with fileClose() when done, or hold on to it and release it later with objRelease().

Common flag combinations:

  • FS_Read - Open existing file for reading
  • FS_Write - Open existing file for writing
  • FS_Read | FS_Write - Open existing file for read/write
  • FS_Write | FS_Create - Open or create file for writing
  • FS_Overwrite - Create new or truncate existing file
  • FS_Read | FS_Write | FS_Create - Open or create for read/write

The FS_Lock flag requests exclusive write access. On Windows, other processes can still read the file but cannot write. On Unix, this is advisory locking.

Parameters
pathPath to the file to open (can be relative or absolute)
flagsCombination of FSOpenFlags specifying open mode
Returns
File handle on success, NULL on failure. Common failures: file doesn't exist (without FS_Create), no permission, path is a directory, disk full (with FS_Create)

Example:

FSFile *f = fsOpen(_SL("data.bin"), FS_Read);
if (f) {
// ... read operations ...
fileClose(&f);
}
@ FS_Read
Open for reading.
Definition fs.h:321

◆ fsRead()

bool fsRead ( FSFile file,
void *  buf,
size_t  sz,
size_t *  bytesread 
)
inline

Reads data from a file

Reads up to 'sz' bytes from the current file position into the buffer. The file position is advanced by the number of bytes actually read. Reading less than requested is not an error - it indicates end-of-file or incomplete data available.

Parameters
fileOpen file handle (must have FS_Read flag)
bufBuffer to receive the data (must be at least 'sz' bytes)
szMaximum number of bytes to read
bytesreadReceives the actual number of bytes read (can be 0 at EOF)
Returns
true if the read operation succeeded (even if 0 bytes read due to EOF), false if an I/O error occurred (bytesread will be set to 0)

Example:

uint8 buffer[1024];
size_t n;
if (fsRead(file, buffer, sizeof(buffer), &n)) {
// successfully read n bytes (0 means EOF)
if (n > 0) {
// process buffer...
}
}
bool fsRead(FSFile *file, void *buf, size_t sz, size_t *bytesread)
Definition file.h:126

Definition at line 126 of file file.h.

References fileRead.

◆ fsSeek()

int64 fsSeek ( FSFile file,
int64  off,
FSSeekType  seektype 
)
inline

Changes the current file position

Moves the file position to a new location for subsequent read/write operations. The position can be set relative to the beginning, current position, or end of the file.

Parameters
fileOpen file handle
offOffset in bytes (can be negative for FS_Cur and FS_End)
seektypeReference point: FS_Set (start), FS_Cur (current), FS_End (end)
Returns
New file position in bytes from start of file, -1 on error (invalid position, unseekable file)

Examples:

fsSeek(file, 0, FS_Set); // Seek to beginning
fsSeek(file, 100, FS_Cur); // Skip forward 100 bytes
fsSeek(file, 0, FS_End); // Seek to end
fsSeek(file, -10, FS_End); // Seek to 10 bytes before end
fsSeek(file, 1024, FS_Set); // Seek to absolute position 1024
int64 fsSeek(FSFile *file, int64 off, FSSeekType seektype)
Definition file.h:225
@ FS_Set
Seek from beginning of file (absolute position)
Definition fs.h:333
@ FS_End
Seek from end of file (usually negative offset)
Definition fs.h:335
@ FS_Cur
Seek from current file position (relative)
Definition fs.h:334

Definition at line 225 of file file.h.

References fileSeek.

◆ fsTell()

int64 fsTell ( FSFile file)
inline

Gets the current file position

Returns the current byte offset within the file. This is the position where the next read or write operation will occur. The position starts at 0 (beginning of file) when the file is opened.

Parameters
fileOpen file handle
Returns
Current file position in bytes (0 = start of file), -1 on error

Definition at line 198 of file file.h.

References fileTell.

◆ fsWrite()

bool fsWrite ( FSFile file,
const void *  buf,
size_t  sz,
size_t *  byteswritten 
)
inline

Writes data to a file

Writes 'sz' bytes from the buffer to the file at the current position. The file position is advanced by the number of bytes written.

On most systems, this function will write all requested bytes or fail. However, on some systems (particularly networked filesystems), a short write may occur. Check byteswritten to verify the full write completed.

Parameters
fileOpen file handle (must have FS_Write flag)
bufBuffer containing data to write
szNumber of bytes to write
byteswrittenOptional pointer to receive actual bytes written (can be NULL)
Returns
true if the write succeeded (check byteswritten for actual amount), false if an I/O error occurred. Common failures: disk full, quota exceeded, permission denied

Example:

uint8 data[] = { 1, 2, 3, 4 };
size_t written;
if (fsWrite(file, data, sizeof(data), &written) && written == sizeof(data)) {
// all data written successfully
}
bool fsWrite(FSFile *file, const void *buf, size_t sz, size_t *byteswritten)
Definition file.h:160

Definition at line 160 of file file.h.

References fileWrite.

◆ fsWriteString()

bool fsWriteString ( FSFile file,
strref  str,
size_t *  byteswritten 
)
inline

Writes a string to a file

The string contents are written as-is, with no line ending added.

Parameters
fileOpen file handle (must have FS_Write flag)
strString to write
byteswrittenOptional pointer to receive actual bytes written (can be NULL)
Returns
true on success, false on I/O error

Definition at line 179 of file file.h.

References fileWriteString.