How to use savefiletostring to append ?.

Hi,

I am new to C++ and UE4 but am trying to learn as I go (tutorials, books etc) but come from procedual languages so may have a few disconnects on concepts.

I can use the following, which is well documented, to save or overwrite a file but would prefer to append in some cases.


FFileHelper::SaveStringToFile(SaveText, *(FPaths::GameDir() + OutFile))

"

Looking at the header file FileHealper.h it defines SaveStringToFile as having some more parameters.


static bool SaveStringToFile( const FString& String, const TCHAR* Filename, EEncodingOptions::Type EncodingOptions=EEncodingOptions::AutoDetect, IFileManager* FileManager=&IFileManager::Get(), uint32 WriteFlags = 0 );


EencodingOptions is defined as detailed below and I understand I need to add 0x08 for an "append" flag.

	struct EEncodingOptions
	{
		enum Type
		{
			AutoDetect,
			ForceAnsi,
			ForceUnicode,
			ForceUTF8,
			ForceUTF8WithoutBOM
		};
	};

Unfortunately I am unclear on how to set / reference the EEncodingOptions AutoDetect type or what is required to satisfy the “IFileManager* FileManager=&IFileManager::Get()” requirements (or tell the function to use its defaults.

Any help would be most welcome.

The EncodingOptions argument is for specifying how text should be represented as bytes in the written file, not for specifying in what mode the file should be written. What you want to do is use the WriteFlags argument to specify in what mode the file should be opened. To append text to a file, use the FILEWRITE_Append flag.

So something like this:



FFileHelper::SaveStringToFile(SaveText, *(FPaths::GameDir() + OutFile), EEncodingOptions::AutoDetect, &IFileManager::Get(), FILEWRITE_Append);


1 Like

Hi,

Thanks or the reply.

Unfortunately that results in;


error C2653: 'EEncodingOptions': is not a class or namespace name
error C2065: 'AutoDetect': undeclared identifier

Ah, the EEncodingOptions enum is part of the FFileHelper struct, which is why the declaration of SaveStringToFile can directly reference it, while your own code needs to explicitly state the full namespace. My mistake. That code snippet should be:



FFileHelper::SaveStringToFile(SaveText, *(FPaths::GameDir() + OutFile), FFileHelper::EEncodingOptions::AutoDetect, &IFileManager::Get(), FILEWRITE_Append);


1 Like