# How can I write to text files?

**URL:** <https://forums.unrealengine.com/t/how-can-i-write-to-text-files/280116>\
**Category:** UI\
**Tags:** text, unreal-engine, CPP, question\
**Created:** [April 9, 2014, 8:49pm UTC](https://forums.unrealengine.com/t/how-can-i-write-to-text-files/280116 "2014-04-09T20:49:39Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [April 9, 2014, 9:39pm UTC](https://forums.unrealengine.com/t/how-can-i-write-to-text-files/280116/2 "2014-04-09T21:39:12Z")

</div>

#CoreMisc.h

\_\_

I thus make your entire project super happy fun with this one .h file !!!

* * *

Read all the functions here!!!

CoreMisc.h

```
/**
	 * Parses a string into tokens, separating switches (beginning with - or /) from
	 * other parameters
	 *
	 * @param	CmdLine the string to parse
	 * @param	Tokens [out] filled with all parameters found in the string
	 * @param	Switches	[out] filled with all switches found in the string
	 */
	static void Parse(const TCHAR* CmdLine, TArray<FString>& Tokens, TArray<FString>& Switches);

/**
	 * Load a text file to an FString.
	 * Supports all combination of ANSI/Unicode files and platforms.
	 * @param Result string representation of the loaded file
	 * @param Filename name of the file to load
	 * @param VerifyFlags flags controlling the hash verification behavior ( see EHashOptions )
	 */
	static bool LoadFileToString( FString& Result, const TCHAR* Filename, uint32 VerifyFlags=0 );

/**
	 * Write the FString to a file.
	 * Supports all combination of ANSI/Unicode files and platforms.
	 */
	static bool SaveStringToFile( const FString& String, const TCHAR* Filename, EEncodingOptions::Type EncodingOptions=EEncodingOptions::AutoDetect, IFileManager* FileManager=&IFileManager::Get() );

/**
	 *	Load the given ANSI text file to an array of strings - one FString per line of the file.
	 *	Intended for use in simple text parsing actions
	 *
	 *	@param	InFilename The text file to read, full path
	 *	@param	InFileManager The filemanager to use - NULL will use &IFileManager::Get()
	 *	@param	OutStrings The array of FStrings to fill in
	 *
	 *	@return	bool true if successful, false if not
	 */
	static bool LoadANSITextFileToStrings(const TCHAR* InFilename, IFileManager* InFileManager, TArray<FString>& OutStrings);

```

#Save Text Files Anywhere You Want

You can save a text file anywhere you want !

* * *

**You can also load and PARSE a string!!!**

This parsing should make your entire goal great fun 🙂

* * *

#FileHelper

You need to preface the above functions with FFileHelper::

#New Method of File Management

My code below is old

see my tutorial here for the new File Management functions

GFileManager no longer exists

**Wiki link**

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

#My Function For You

```
bool UVictoryBPFunctionLibrary::SaveStringTextToFile(
	FString SaveDirectory, 
	FString FileName, 
	FString SaveText,
	bool AllowOverWriting
){
	//GFileManager?
	if (!GFileManager) return false;
 
	//Dir Exists?
	if ( !GFileManager->DirectoryExists( *SaveDirectory))
	{
		//create directory if it not exist
		GFileManager->MakeDirectory( *SaveDirectory);
 
		//still could not make directory?
		if (!GFileManager->DirectoryExists( *SaveDirectory))
		{
			//Could not make the specified directory
			return false;
			// ~~~~~~~~~~~~~~~~~~~~~~
		}
	}
 
	//get complete file path
	SaveDirectory += "\\";
	SaveDirectory += FileName;
 
	//No over-writing?
	if (!AllowOverWriting)
	{
		//Check if file exists already
		if (GFileManager->GetFileAgeSeconds( * SaveDirectory) > 0) 
		{
			//no overwriting
			return false;
		}
	}
 
	return FFileHelper::SaveStringToFile(SaveText, * SaveDirectory);
}

```

#LoadStringFromTextFile

```
/**
	 * Load a text file to an FString.
	 * Supports all combination of ANSI/Unicode files and platforms.
	 * @param Result string representation of the loaded file
	 * @param Filename name of the file to load
	 * @param VerifyFlags flags controlling the hash verification behavior ( see EHashOptions )
	 */
	static bool LoadFileToString( FString& Result, const TCHAR* Filename, uint32 VerifyFlags=0 );
```

---

_[View the full topic](https://forums.unrealengine.com/t/how-can-i-write-to-text-files/280116)._
