Download

Load a Text File

Hello,

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



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

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

Thanks, really cool :slight_smile:

I have tryed your Code, but have a Problem


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:


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

What should i do?

you should post more of your compile error :slight_smile:

Alrigh, here the Code:


// 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


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 :smiley:

Oh, i just found the Error by myself :smiley:

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



                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