# Load a Text  File

**URL:** <https://forums.unrealengine.com/t/load-a-text-file/1946>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [April 5, 2014, 11:45pm UTC](https://forums.unrealengine.com/t/load-a-text-file/1946 "2014-04-05T23:45:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cybergebi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cybergebi](https://forums.unrealengine.com/u/Cybergebi)\
**Post date:** [April 5, 2014, 11:45pm UTC](https://forums.unrealengine.com/t/load-a-text-file/1946/1 "2014-04-05T23:45:35Z")

</div>

Hello,

i want to load a Textfile, i only know the Method with fstream

```auto

std::ifstream ifs("C:\myfile.txt");
std::string content((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));

```

But that doesn’t seem to work and i also need i need a FString.

How can i read a File and load it into a String?

Thanks  
Greetings  
Cybergebi

---

<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 5, 2014, 11:50pm UTC](https://forums.unrealengine.com/t/load-a-text-file/1946/2 "2014-04-05T23:50:32Z")

</div>

FFileHelper

You want FFileHelper.h !

Check it out in the Source code!

Here’s my code for saving a text file to hard disk

use this as reference for doing the reverse + FFileHelper.h

> **[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...

---

<div class="post-metadata">

**Author:** ![Cybergebi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cybergebi](https://forums.unrealengine.com/u/Cybergebi)\
**Post date:** [April 6, 2014, 12:14am UTC](https://forums.unrealengine.com/t/load-a-text-file/1946/3 "2014-04-06T00:14:07Z")

</div>

Thanks, really cool 🙂

---

<div class="post-metadata">

**Author:** ![Cybergebi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cybergebi](https://forums.unrealengine.com/u/Cybergebi)\
**Post date:** [April 6, 2014, 1:22am UTC](https://forums.unrealengine.com/t/load-a-text-file/1946/4 "2014-04-06T01:22:24Z")

</div>

> [@Rama;12843](#):
>
> FFileHelper
> 
> You want FFileHelper.h !
> 
> Check it out in the Source code!
> 
> Here’s my code for saving a text file to hard disk
> 
> use this as reference for doing the reverse + FFileHelper.h
> 
> [A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums](https://wiki.unrealengine.com/File_Management,_Create_Folders,_Delete_Files,_and_More#Create_Text_Files)

I have tryed your Code, but have a Problem

```auto

FString CompleteFilePath = "G:\saves\map.txt";
		if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*CompleteFilePath))
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Could not Find File"));
			return;
		}
const int64 FileSize = FPlatformFileManager::Get().GetPlatformFile().FileSize(*CompleteFilePath);

		//if not in player controller use UE_LOG. ClientMessages show up if you press ~ in-game
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("File Size is:"));
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::FromInt(FileSize));

```

i got Following Error:

```auto

error C2220: warning treated as error - no object file generated 

```

What should i do?

---

<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 6, 2014, 5:34am UTC](https://forums.unrealengine.com/t/load-a-text-file/1946/5 "2014-04-06T05:34:22Z")

</div>

you should post more of your compile error 🙂

---

<div class="post-metadata">

**Author:** ![Cybergebi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cybergebi](https://forums.unrealengine.com/u/Cybergebi)\
**Post date:** [April 6, 2014, 12:40pm UTC](https://forums.unrealengine.com/t/load-a-text-file/1946/6 "2014-04-06T12:40:40Z")

</div>

> [@Rama;12930](#):
>
> you should post more of your compile error 🙂

Alrigh, here the Code:

```auto

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

#include "MyProjectThirdPersion.h"
#include "SpawnHandler.h"
#include "FileHelpers.h"

ASpawnHandler::ASpawnHandler(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{

}

void ASpawnHandler::BeginPlay()
{

	Super::BeginPlay();
	if (GEngine)
	{
		FString CompleteFilePath = "G:\saves\map.txt";
		if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*CompleteFilePath))
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Could not Find File"));
			return;
		}

		const int64 FileSize = FPlatformFileManager::Get().GetPlatformFile().FileSize(*CompleteFilePath);

		//if not in player controller use UE_LOG. ClientMessages show up if you press ~ in-game
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("File Size is:"));
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::FromInt(FileSize));
		

	}

}

```

and the FULL Error

```auto

Error	5	error : Failed to produce item: C:\Users\Cybergebi\Documents\Unreal Projects\MyProjectThirdPersion\Binaries\Win64\UE4Editor-MyProjectThirdPersion.pdb	C:\Users\Cybergebi\Documents\Unreal Projects\MyProjectThirdPersion\Intermediate\ProjectFiles\ERROR
Error	2	error C2220: warning treated as error - no 'object' file generated	C:\Users\Cybergebi\Documents\Unreal Projects\MyProjectThirdPersion\Source\MyProjectThirdPersion\SpawnHandler.cpp: 19
Error	6	error MSB3073: The command "G:\UnrealEngine\Engine\Build\BatchFiles\Build.bat MyProjectThirdPersionEditor Win64 Development "C:\Users\Cybergebi\Documents\Unreal Projects\MyProjectThirdPersion\MyProjectThirdPersion.uproject"" exited with code -1.	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets:	38
Warning	1	warning : Was not able to delete temporary file created by Unreal Build Tool: H:\Local\Temp	mpD196.tmp.bat	C:\Users\Cybergebi\Documents\Unreal Projects\MyProjectThirdPersion\Intermediate\ProjectFiles\EXEC
Warning	4	warning C4129: 'm' : unrecognized character escape sequence	C:\Users\Cybergebi\Documents\Unreal Projects\MyProjectThirdPersion\Source\MyProjectThirdPersion\SpawnHandler.cpp:	19	
Warning	3	warning C4129: 's' : unrecognized character escape sequence	C:\Users\Cybergebi\Documents\Unreal Projects\MyProjectThirdPersion\Source\MyProjectThirdPersion\SpawnHandler.cpp:	19

```

Thanks for helping me 😃

---

<div class="post-metadata">

**Author:** ![Cybergebi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cybergebi](https://forums.unrealengine.com/u/Cybergebi)\
**Post date:** [April 6, 2014, 12:46pm UTC](https://forums.unrealengine.com/t/load-a-text-file/1946/7 "2014-04-06T12:46:25Z")

</div>

Oh, i just found the Error by myself 😃

For others who are also Searching for a Solution, the Read Code :rolleyes:

```auto

                FString CompleteFilePath = "G:\\saves\\map.txt";
		if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*CompleteFilePath))
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Could not Find File"));
			return;
		}

		const int64 FileSize = FPlatformFileManager::Get().GetPlatformFile().FileSize(*CompleteFilePath);

		//if not in player controller use UE_LOG. ClientMessages show up if you press ~ in-game
		
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::FromInt(FileSize));
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("File Size is: (in kb)"));
		FString FileData = "TEST";
		FFileHelper::LoadFileToString(FileData, *CompleteFilePath);
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FileData);

```

Thanks for all

Greetings  
Cybergebi

---

<div class="post-metadata">

**Author:** ![I\_Jaba](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/i_jaba/32/718087_2.png) [@I\_Jaba](https://forums.unrealengine.com/u/I_Jaba)\
**Post date:** [June 16, 2023, 6:08pm UTC](https://forums.unrealengine.com/t/load-a-text-file/1946/9 "2023-06-16T18:08:17Z")

</div>

> [@Cybergebi](#):
>
> `GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FileData);`

Works flawlessly. Thank you very much.

Here is the function I wrote to read a file from an absolute path :

```auto
FString UBPFL_FileIO::LoadFileToString(FString AbsolutePath)
{	
	FString result = "NONE";

	FFileHelper::LoadFileToString(result, *AbsolutePath); 
    //Reads file content and puts it inside result

    return result;
}

```
