File

Inherited: None

Description

The File class provides an interface for reading from and writing to files. File expects the file separator to be ‘/’ regardless of operating system. The use of other separators (e.g., ‘’) is not supported.

You can check for a file’s existence using _exists(), and remove a file using _delete(). You can create a directory using _mkdir(), list all files in directory using _flist() and retrive other basic information.

The file can be opened with _open() and closed with _fclose(). Data is usually can be read with _fread() and written with _fwrite().

Common usecase:

File *file = Engine::file();
_FILE *fp   = file->_fopen("filename", "r");
if(fp) {
    ByteArray data;
    data.resize(file->_fsize(fp));
    file->_fread(&data[0], data.size(), 1, fp);
    file->_fclose(fp);
}

Public Methods

bool

exists (const char * path)

int

fclose (_FILE * stream)

bool

fdelete (const char * path)

void

finit (const char * argv0)

StringList

flist (const char * path)

_FILE *

fopen (const char * path, const char * mode)

_size_t

fread (void * ptr, _size_t size, _size_t count, _FILE * stream)

void

fsearchPathAdd (const char * path, bool isFirst = false)

_size_t

fsize (_FILE * stream)

_size_t

ftell (_FILE * stream)

_size_t

fwrite (const void * ptr, _size_t size, _size_t count, _FILE * stream)

bool

isdir (const char * path)

bool

mkdir (const char * path)

Static Methods

None

Methods Description

bool File::exists (char * path)

Checks if a file by path exists. Returns true if operation succeeded; otherwise returns false.


int File::fclose (_FILE * stream)

Closes file stream. Returns 0 if succeeded; otherwise returns non-zero value.


bool File::fdelete (char * path)

Delete file. Returns true if the operation succeeded; otherwise returns false.

Note: The file can be deleted only if path marked as writable.


void File::finit (char * argv0)

Initialize the file system module at argv0 application file path. This method must be called before any operations with filesytem.

Note: Usually, this method calls internally and must not be called manually.


StringList File::flist (char * path)

Get a file listing of a search path directory.

StringList rc = file->_flist("savegames");

for(auto it : rc) {
    printf("Found - [%s].\n", it.c_str());
}

:ref:`_FILE<api__FILE>`* File::fopen (char * path, char * mode)

Opens the file whose name is specified in the path and associates it with a stream that can be identified in future operations. The operations that are allowed on the stream and how these are performed are defined by the mode parameter. Allowed values of mode parameter:

“r” - Open a file for reading. “w” - Open a file for writing. The path must marked as writable. “a” - Open a file for appending. The path must marked as writable.

Returns _FILE pointer to file stream if succeeded; otherwise returns nullptr value.


_size_t File::fread (void * ptr, _size_t size, _size_t count, _FILE * stream)

Reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr. The file must be opened for reading.

Returns number of objects read.


void File::fsearchPathAdd (char * path, bool isFirst = false)

Add an archive or directory to the search path. If isFirst provided as true the directory will be marked as writable. The Method can be called multiple time to add more directories to work with.

Note: Usually, this method calls internally and must not be called manually.


_size_t File::fsize (_FILE * stream)

Get total length of a file stream in bytes.


_size_t File::ftell (_FILE * stream)

Determine current position within a file stream.

Returns offset in bytes from start of file.


_size_t File::fwrite (void * ptr, _size_t size, _size_t count, _FILE * stream)

Writes an array of count elements, each one with a size of size bytes, from the block of memory pointed by ptr to the current position in the stream. The file must be opened for writing.

Returns number of objects written.


bool File::isdir (char * path)

Determine if a file by path in the search path is really a directory.

Returns true if operation succeeded; otherwise returns false.


bool File::mkdir (char * path)

Create directory. Returns true if the operation succeeded; otherwise returns false.

Note: Directory can be created only if path marked as writable.