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.
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?
Hello,
FFileHelper::SaveStringToFile() has 5 arguments:
const FString & String,
const TCHAR* Filename,
EEncodingOptions::Type EncodingOptions,
IFileManager * FileManager,
uint32 WriteFlags
Last three of them are not obligatory, but you can use them if you need more custom setup. The last parameter is WriteFlags, there are several of them defined in FileManager:
enum EFileWrite
{ FILEWRITE_None = 0x00,
FILEWRITE_NoFail = 0x01,
FILEWRITE_NoReplaceExisting = 0x02,
FILEWRITE_EvenIfReadOnly = 0x04,
FILEWRITE_Append = 0x08,
FILEWRITE_AllowRead = 0x10
};
To append you can use FILEWRITE_Append.
Here is the example of call for append: *FFileHelper::SaveStringToFile(StringToSave, PathToFile, FFileHelper::EEncodingOptions::AutoDetect, &IFileManager::Get(), EFileWrite::FILEWRITE_Append)
LDOGLONDON : try adding an escape return character at the beginning or end of your string depending on what you need. I found that “\n” works when opening as a .csv but not as a text file but “\r\n” seems to work correctly.
So for example if you did:
“First line \r\n Second line”
It would look like :
First line
Second line
and if you are appending on to an existing file and seeing this:
Thanks, yeah I eventually got it to work doing that. I was being thrown off because I was deleting everything in the file I was writing to, then testing it so it would write to that file again. And for some reason I got like Chinese letters, but all I had to do to fix the problem was delete the file and let the program generate a new one. Then the newline and everything worked fine.