Append to text file instead of writing over whats there

Hello! I have been following Rama’s tutorial on how to write to a text file from UE. ( Link ) It is a very helpful walkthrough, but I am wondering if there is a way to append to the text file instead of writing over what is already there. I have been writing to the file every 10 seconds and make sure to print my the RunTime so I can see that it is indeed overwriting what is currently there. (Spoiler: it is)

The code below has been writing a single line to the file, overwriting what is already there.


FFileHelper::SaveStringToFile(SaveText, * SaveDirectory);

The code below is able to write the string twice on the same line, but I was reading that I have to delete the file handle before quitting my program. Is there an “OnQuit” function available where I can delete it?


IPlatformFile& file = FPlatformFileManager::Get().GetPlatformFile();
IFileHandle* handle = file.OpenWrite(*directory);
if (handle)
{
	handle->Write((const uint8*)TCHAR_TO_ANSI(*string), string.Len());
	handle->Write((const uint8*)TCHAR_TO_ANSI(*string), string.Len());
	delete handle;
}

Also, my string has a
in it but doesn’t seem to add a new line… is there something else I have to add?

All help is greatly appreciated!

Welcome to the forums.

To answer your question, OpenWrite has two more arguments which are optional. The first of them being bAppend which defaults to false when it is not passed like you’re doing it currently. Simply add true as a second argument and you should be able to append. Not entirly certain what happens accross multiple sessions i.e. you may have to clear the file first if your using the same one or simply use a differrent file path every time.

There is probably something like OnQuit but I do not know where or what it is called.

I would have expected
to work as well not sure about that.

On Windows, to add a new line, you need to use
, there is a define setup in Unreal called LINE_TERMINATOR_TEXT that you can use to get the appropriate line ending for whatever platform you are building on.