Working with Files

Reading and writing files using the System::IO namespace.

Includes

#include <System/IO/FileStream.hpp>
#include <System/IO/StreamReader.hpp>
#include <System/IO/StreamWriter.hpp>
#include <System/IO/BinaryReader.hpp>
#include <System/IO/BinaryWriter.hpp>
#include <System/IO/File.hpp>
#include <System/IO/Path.hpp>

Reading a Text File

// Method 1: StreamReader (line by line)
{
    auto reader = std::make_unique<System::IO::StreamReader>("data.txt");
    std::string line;
    while ((line = reader->ReadLine()) != "") {
        std::cout << line << "\n";
    }
    reader->Close();
}

// Method 2: File::ReadAllText (full file at once)
std::string content = System::IO::File::ReadAllText("data.txt");

// Method 3: File::ReadAllLines
std::vector<std::string> lines = System::IO::File::ReadAllLines("data.txt");

Writing a Text File

// Method 1: StreamWriter
{
    auto writer = std::make_unique<System::IO::StreamWriter>("output.txt");
    writer->WriteLine("Hello, world!");
    writer->WriteLine("Line 2");
    writer->Close();
}

// Method 2: File::WriteAllText
System::IO::File::WriteAllText("output.txt", "Hello, world!\n");

// Method 3: File::WriteAllLines
std::vector<std::string> lines = {"Line 1", "Line 2", "Line 3"};
System::IO::File::WriteAllLines("output.txt", lines);

Binary File I/O

// Write binary
{
    auto fs = std::make_unique<System::IO::FileStream>(
        "data.bin", System::IO::FileMode::Create);
    auto writer = std::make_unique<System::IO::BinaryWriter>(std::move(fs));
    writer->Write(42);           // int
    writer->Write(3.14f);        // float
    writer->Write(std::string("hello")); // length-prefixed string
    writer->Close();
}

// Read binary
{
    auto fs = std::make_unique<System::IO::FileStream>(
        "data.bin", System::IO::FileMode::Open);
    auto reader = std::make_unique<System::IO::BinaryReader>(std::move(fs));
    int    i = reader->ReadInt32();
    float  f = reader->ReadSingle();
    std::string s = reader->ReadString();
    reader->Close();
}

Path Manipulation

// Combine paths
std::string full = System::IO::Path::Combine("data", "saves", "save1.dat");

// Get filename / extension
std::string name = System::IO::Path::GetFileName("data/saves/save1.dat"); // "save1.dat"
std::string ext  = System::IO::Path::GetExtension("save1.dat");           // ".dat"
std::string base = System::IO::Path::GetFileNameWithoutExtension("save1.dat"); // "save1"

// Directory
std::string dir = System::IO::Path::GetDirectoryName("data/saves/save1.dat"); // "data/saves"

File Existence and Directory Operations

// Check existence
bool exists = System::IO::File::Exists("data.txt");
bool dirEx  = System::IO::Directory::Exists("data/saves");

// Create directory
System::IO::Directory::CreateDirectory("data/saves");

// Delete file
System::IO::File::Delete("old.txt");

// List files
std::vector<std::string> files = System::IO::Directory::GetFiles("data");
std::vector<std::string> dirs  = System::IO::Directory::GetDirectories("data");

IsolatedStorage

For sandboxed storage (used in mobile-eggbert), use IsolatedStorageFile:

#include <System/IO/IsolatedStorage/IsolatedStorageFile.hpp>

auto store = System::IO::IsolatedStorage::IsolatedStorageFile::GetUserStoreForApplication();
// Creates/opens files relative to the app's isolated storage root

On Android with SDL3, the root path is obtained via SDL_GetPrefPath (SDL3 must be provided by the parent project, e.g., CNA).

Exception handling
File I/O operations can throw System::IO::IOException and its subclasses (FileNotFoundException, DirectoryNotFoundException). Wrap in try/catch when robustness is needed.