Append to text file instead of overwrite 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 \n in it but doesn’t seem to add a new line… is there something else I have to add?

All help is greatly appreciated!

Hello,
FFileHelper::SaveStringToFile() has 5 arguments:

  1. const FString & String,
  2. const TCHAR* Filename,
  3. EEncodingOptions::Type EncodingOptions,
  4. IFileManager * FileManager,
  5. 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)

2 Likes

I’m doing this, but when I append a string it doesn’t add a newline. Is there anything I can do to add a newline?

Thanks mcbernykh!

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:

Original line.First line

Then try:
“\r\n First line”

and it should come in as :

Original line.

First line.

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.