CLua Documentation
📄 Working with Text Files
CLua includes simple functions for reading and writing text files in various encodings. All file operations are restricted to the program's directory — working outside its structure is not allowed.
💾 Saving Text to a File
SaveFile("data/test.txt", "File content", Encoding.UTF8);
- The first parameter is the relative file path.
- The second is the text content.
- The third specifies the encoding (see the table below).
📥 Loading a Text File
local content = LoadFile("data/test.txt", Encoding.UTF8);
- Returns the file content as a string.
- If encoding is not specified, automatic detection is attempted.
🔎 Checking if a File Exists
if FileExists("data/test.txt") then
print("File exists!");
end;
- Returns a boolean value.
🧾 Available Encodings
Encoding = {
ANSI = "ansi",
UTF8 = "utf8",
UTF8BOM = "utf8bom",
UTF16 = "utf16",
UTF16LE = "utf16le",
UTF16BE = "utf16be",
Unicode = "utf16le" -- Alias
};
🔠 Special Characters
CR = string.char(13); -- CR - \r (Line end on pre-OSX Mac)
LF = string.char(10); -- LF - \n (Line end on Unix/macOS)
CRLF = CR .. LF; -- CR + LF - \r\n (Line end on Windows)
t = string.char(9); -- Tab
Q = string.char(34); -- "
a = string.char(39); -- '
📁 Listing Files in a Directory
local files = GetFiles("data/units")
-- returns a table of filenames in the given folder
local files = GetFiles("data/units", "lua")
-- filter by extension (without the dot)
local files = GetFiles("data/units", "lua", true)
-- strip extensions from returned names
- The first parameter is the relative path to the folder.
- The second parameter (optional) filters results by file extension.
- The third parameter (optional, default
false) — iftrue, file extensions are stripped from the returned names. - Returns an empty table if the directory does not exist.
- Files are returned as names only (not full paths).
📂 Listing Subdirectories
local dirs = GetDirs("data")
-- returns a table of subdirectory names inside the given folder
- The parameter is the relative path to the folder.
- Returns an empty table if the directory does not exist.
- Returns directory names only (not full paths).
Note: These functions cannot operate on files outside the program directory (for security reasons).