Skip to content

File IO C++ Reference ​

Load, save, and append text files from C++ with UFileIO.

Header: FileIO.h · Class: UFileIO

Each function takes an EFileLocation that picks the base directory, and a file name relative to it. For more on how the files are written, see the File IO nodes.

NameDescription
LoadFileToStringReads a text file into an FString.
SaveTextToFileWrites text to a file, replacing any existing contents.
AppendTextToFileAdds text to the end of a file, creating it if needed.
EFileLocationThe base directory for the file.

EFileLocation ​

cpp
enum class EFileLocation : uint8 { ProjectSaved, UserDocuments, ProjectContent };
ValueBase directory
EFileLocation::ProjectSavedFPaths::ProjectSavedDir(), your project's Saved folder.
EFileLocation::UserDocumentsFPlatformProcess::UserDir(), the user's Documents folder. On Android, this is /storage/emulated/0/Documents/.
EFileLocation::ProjectContentFPaths::ProjectContentDir(), your project's Content folder.

The full path is the base directory combined with the file name using FPaths::Combine, so the file name can include subfolders, like Logs/DebugDump.txt.

LoadFileToString ​

cpp
static bool UFileIO::LoadFileToString(EFileLocation LoadLocation, FString FileName, FString& OutString, FString& OutFilePath);

Reads a text file and returns its contents.

NameDescription
LoadLocationThe base directory to load from.
FileNameThe file to read, including its extension. Taken by value.
OutStringReceives the contents of the file.
OutFilePathReceives the full path the function tried to read, whether or not it succeeded.

Returns: true if the file was read successfully.

cpp
#include "FileIO.h"

FString Contents;
FString FilePath;
if (UFileIO::LoadFileToString(EFileLocation::ProjectSaved, TEXT("Config/Settings.txt"), Contents, FilePath))
{
    // Use Contents
}

SaveTextToFile ​

cpp
static bool UFileIO::SaveTextToFile(EFileLocation SaveLocation, const FString& FileName, const FString& Content, FString& OutFullPath);

Writes text to a file, replacing any existing contents.

NameDescription
SaveLocationThe base directory to save to.
FileNameThe file to write, including its extension.
ContentThe text to write.
OutFullPathReceives the full path the file was written to.

Returns: true if the file was saved successfully.

cpp
FString SavedPath;
UFileIO::SaveTextToFile(EFileLocation::ProjectSaved, TEXT("Logs/DebugDump.txt"), DumpText, SavedPath);

SavedPath receives the full path, and the Output Log shows:

LogTemp: Successfully saved file to: D:/MyGame/Saved/Logs/DebugDump.txt

WARNING

SaveTextToFile overwrites existing files without asking. Use AppendTextToFile to keep existing content.

AppendTextToFile ​

cpp
static bool UFileIO::AppendTextToFile(EFileLocation SaveLocation, const FString& FileName, const FString& Content, FString& OutFullPath, const bool bAddLineBreak = true);

Adds text to the end of a file, creating the file if it doesn't exist.

NameDescription
SaveLocationThe base directory to save to.
FileNameThe file to append to, including its extension.
ContentThe text to append.
OutFullPathReceives the full path the file was written to.
bAddLineBreakWhen true (the default), adds a line feed (\n) after Content.

Returns: true if the text was written successfully.

cpp
FString EventsPath;
UFileIO::AppendTextToFile(EFileLocation::ProjectSaved, TEXT("Logs/Events.txt"), TEXT("Level loaded"), EventsPath);

NOTE

Every UFileIO function writes a success or failure message, including the full path, to the Output Log under LogTemp.