File manipulation: Read,Write

From UE4: Community Wiki
Jump to: navigation, search


This article is a preliminary version, it is subject to change.


Introduction[edit]

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[edit]

In this article, we will use FFileHelper. FFileHelper is platform-agnostic, which means it should work on all platforms you publish on. It has some great features that make reading and writing a file super easy. With FFileHelper you can load file's directly into an FString. And turn a FString into a file.



Loading a file into a string.[edit]

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.[edit]

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.

//We need to locate the file.
FString file = FPaths::ProjectConfigDir();
file.Append(TEXT("MyConfig.txt"));

// We will use this FileManager to deal with the file itself(Not with the content of 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[edit]

This will replace everything in the file

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

To do this we use the function:

::SaveStringArrayToFile(TArray<FString> 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<FString> 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 the 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


Loading a file to a FString array[edit]

The last function was showing us how to write a string array to a file. Now at some point, you might want to load/Read this string array. We can do that with this function.

LoadFileToStringArray(TArray<FString>&,TCHAR*)

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<FString> StringsFromFile;

// 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::LoadFileToStringArray(StringsFromFile,*file))
   {
      for (FString string : StringsFromFile)
      {
         UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Found string %s"),*string);   
      }

   }
   else
   {
      UE_LOG(LogTemp, Warning, TEXT("FileManipulation: Failed Read string array."));
   }
}
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 exists and I got this in my LOG

LogTemp: Warning: FileManipulation: Found string Hello
LogTemp: Warning: FileManipulation: Found string World.
LogTemp: Warning: FileManipulation: Found string From
LogTemp: Warning: FileManipulation: Found string Unreal
LogTemp: Warning: FileManipulation: Found string Engine 4

Documentation[edit]

Great with this, you should be able to manipulate your file's. If you are having any trouble, consider taking a look at the official documentation.


Conclusion[edit]

Using these few functions should give you a good start at manipulating the content of files. They are relatively high level, and the fact that they are platform-agnostic makes them super convenient. You do need to be aware that these functions override the content of the files. So test them on file's that are not important.