Append when writing to a file.


I’ve create a simple game which offers the player a widget were he can type down some text and them saves it to a file.
It’s working fine, the only problem is that the files gets replaced instead of appended and I’m not being able to figure out how to append. Can you guys help me out?

Here is my code: .h

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "Paths.h"
#include "WriteToFile.generated.h"

class UWriteToFile : public UBlueprintFunctionLibrary

	UFUNCTION(BlueprintCallable, meta = (HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject", FriendlyName = "File-IO"), Category = "WriteToFile")
		static bool FileIO__SaveStringTextToFile(FString SaveDirectory, FString fileName, FString SaveText, bool AllowOverWriting);


Code: .cpp

#include "WriteToFile.h"

UWriteToFile::UWriteToFile(const class FObjectInitializer& PCIP)


bool UWriteToFile::FileIO__SaveStringTextToFile(FString SaveDirectory, FString fileName, FString SaveText, bool AllowOverWriting)
	FString path;
	path = FPaths::GameDir();
	path += "/Questions";

	if (!FPlatformFileManager::Get().GetPlatformFile().DirectoryExists(*path))
		if (!FPlatformFileManager::Get().GetPlatformFile().DirectoryExists(*path))
			return false;

	path += "/";
	path += fileName;

	if (!AllowOverWriting)
		if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*path))
			return false;

	return FFileHelper::SaveStringToFile(SaveText, *path);

Any idea what I am missing?

You use FFileHelper::SaveStringToFile function, it’s allow only overriding file. You need to write your own version of this function with appending behavior.
Heres located source for SaveStringToFile function, you need this line:

auto Ar = TUniquePtr<FArchive>( FileManager->CreateFileWriter( Filename, 0 ) );

  • those second parameter in CreateFileWriter function is write flag:

enum EFileWrite
	FILEWRITE_NoFail            = 0x01,
	FILEWRITE_NoReplaceExisting = 0x02,
	FILEWRITE_EvenIfReadOnly    = 0x04,
	FILEWRITE_Append			= 0x08,
	FILEWRITE_AllowRead			= 0x10

  • so you need to change it to something like this:

auto Ar = TUniquePtr<FArchive>( FileManager->CreateFileWriter( Filename, EFileWrite::FILEWRITE_Append ) );

or even better to add this write flag as a parameter to your version of SaveStringToFile function.

Or you can read your file, append new data to it, and write data(with appendix) to file again using FFileHelper::SaveStringToFile.