File manipulation: Read,Write

Introduction In this article, I will show you how to Read Manipulate and write to a file. We will be focusing on the content of a file. If you want to do something like to Create, find, Copy, Or De...

Updated 9 months ago Edit Page Revisions

Introduction

In this article, I will show you how to Read Manipulate and write to a file. We will be focusing on the content of a file. If you want to do something like to Create, find, Copy, Or Delete a file Please take a look at this article. File and Folder Management, Create, Find, Delete

File Helper

In this article, we will use FFileHelper. It has some great features that make reading and writing a file super easy.

Loading a file into a string.

The quickest and most easy way to read a file to string is using the function:


FFileHelper::LoadFileToString(&FString Result,TCHAR* FileDestination);

Here is an example.


FString file = FPaths::ProjectConfigDir();
file.Append(TEXT("MyConfig.txt"));

// We will use this FileManager to deal with the file.
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();

FString FileContent;
// Always first check if the file that you want to manipulate exist.
if (FileManager.FileExists(*file))
{
   // We use the LoadFileToString to load the file into
      if(FFileHelper::LoadFileToString(FileContent,*file,FFileHelper::EHashOptions::None))
      {
         UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Text From File: %s"), *FileContent);  
      }
      else
      {
         UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Did not load text from file"));
      }
}
else
{
   UE_LOG(LogTemp, Warning, TEXT("FileManipulation: ERROR: Can not read the file because it was not found."));
   UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Expected file location: %s"),*file);
}

I was able to open the file and read it. This is what my log returned.


LogTemp: Warning: FileManipulation: Text From File: Hello world from file

Saving a string into a file.

This will replace everything in the file

Writing an FString to a file is just as easy as reading it.


SaveStringToFile(FString string,FString path)

Here is an example.


 FString file = FPaths::ProjectConfigDir();
file.Append(TEXT("MyConfig.txt"));

// We will use this FileManager to deal with the file.
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();

FString StringToWrite(TEXT("Hello World. Written from Unreal Engine 4"));
// Always first check if the file that you want to manipulate exist.
if (FileManager.FileExists(*file))
{
   // We use the LoadFileToString to load the file into
   if(FFileHelper::SaveStringToFile(StringToWrite,*file))
   {
      UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Sucsesfuly Written: \"%s\" to the text file"),*StringToWrite);
   }
   else
   {
      UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Failed to write FString to file."));
   }
}
else
{
   UE_LOG(LogTemp, Warning, TEXT("FileManipulation: ERROR: Can not read the file because it was not found."));
   UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Expected file location: %s"),*file);
}

The file got written successfully. This is what i got in my log.


LogTemp: Warning: FileManipulation: Sucsesfuly Written: "Hello World. Written from Unreal Engine 4" to the text file

Saving an array to a text file

This will replace everything in the file

This function will take a TArray and write each one of them to a text file on a new line.

To do this we use the function:


::SaveStringArrayToFile(TArray Strings,TCHAR* File)

Here is an example.


 FString file = FPaths::ProjectConfigDir();
file.Append(TEXT("MyConfig.txt"));

// We will use this FileManager to deal with the file.
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();

TArray StringsToWrite;
StringsToWrite.Add(FString(TEXT("Hello")));
StringsToWrite.Add(FString(TEXT("World.")));
StringsToWrite.Add(FString(TEXT("From")));
StringsToWrite.Add(FString(TEXT("Unreal")));
StringsToWrite.Add(FString(TEXT("Engine 4")));

// Always first check if the file that you want to manipulate exist.
if (FileManager.FileExists(*file))
{
   // We use the LoadFileToString to load the file into
   if(FFileHelper::SaveStringArrayToFile(StringsToWrite,*file))
   {
      UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Sucsesfuly Written: \"%d\" FStrings to the text file"),StringsToWrite.Num());
   }
   else
   {
      UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Failed to write FString to file."));
   }
}
else
{
   UE_LOG(LogTemp, Warning, TEXT("FileManipulation: ERROR: Can not read file because it was not found."));
   UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Expected file location: %s"),*file);
}

The file exists. And this is what i got inside.


Hello
World.
From
Unreal
Engine 4