Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

File

Add this in your code:

#include <engine/file_system/file.h>

Description

Class to manage a file (Create, open, read, write).

Public methods


Open

Open the file.
Return true if the file is opened successfully.

Parameters:

  • fileMode: The mode to open the file
bool Open(FileMode fileMode)

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("C:/my_folder/my_file.txt");

if(file->Open(FileMode::ReadOnly))
{
}

CheckIfExist

Check if the file exists.
Return true if the file exists.

bool CheckIfExist()

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("C:/my_folder/my_file.txt");

if(file->CheckIfExist())
{
}

Close

Close file.

bool Close()

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("C:/my_folder/my_file.txt");

if(file->Open(FileMode::ReadOnly))
{
    // ...
    file->Close();
}

Write (String)

Write string data to the file.

Parameters:

  • data: The data to write
void Write(const std::string& data)

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("my_file.txt");

if(file->Open(FileMode::WriteCreateFile))
{
    // ...
    file->Write("Hello World!");
}

Write (Binary)

Write binary data to the file.

Parameters:

  • data: The data to write
  • size: The size of the data in byte
void Write(const unsigned char* data, size_t size)

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("my_file.bin");


if(file->Open(FileMode::WriteCreateFile))
{
    uint32_t value = 44;
    // ...
    file->Write(&value, sizeof(uint32_t));
}

ReadAll

Read all the content of the file as a string.

std::string ReadAll()

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("my_file.txt");

if(file->Open(FileMode::ReadOnly))
{
    std::string content = file->ReadAll();
}

ReadAllBinary

Read all the content of the file as a binary (Need to free the pointer after).

Parameters:

  • size: Output: The size of the binary in byte
unsigned char* ReadAllBinary(size_t& size)

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("my_file.bin");

if(file->Open(FileMode::ReadOnly))
{
    size_t size = 0;
    unsigned char* content = file->ReadAllBinary(size);
    // ...
    free(content);
}

ReadBinary

Read a part of the content of the file as a binary (Need to free the pointer after).

Parameters:

  • offset: Read offset in byte
  • size: The size to read in byte
unsigned char* ReadBinary(size_t offset, size_t size)

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("my_file.bin");

if(file->Open(FileMode::ReadOnly))
{
    unsigned char* content = file->ReadBinary(4, 30);
    // ...
    free(content);
}

GetPath

Get file path.

const std::string& GetPath() const

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("C:/my_folder/my_file.txt");

std::string filePath = file->GetPath(); // = C:/my_folder/my_file.txt

GetFolderPath

Get file folder path.

std::string GetFolderPath() const

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("C:/my_folder/my_file.txt");

std::string filePath = file->GetFolderPath(); // = C:/my_folder/

GetFileName

Get file name.

const std::string& GetFileName() const

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("C:/my_folder/my_file.txt");

std::string filePath = file->GetFileName(); // = my_file

GetFileExtension

Get file extension (dot included).

const std::string& GetFileExtension() const

Code sample:

std::shared_ptr<File> file = FileSystem::MakeFile("C:/my_folder/my_file.txt");

std::string filePath = file->GetFileExtension(); // = .txt