Can´t write nor append for my life a file in an Iphone, even though when doing the same process on Android I could do the same without any further problems. Also, I’m able to read without problems. Using 5.2, both blueprints and c++ code, in this one the FFilehelper library, with the function SaveStringToTile.
Header Content:
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "RWtextFile.generated.h"
class FJsonObject;
/**
*
*/
UCLASS()
class MYAPP_API URWtextFile : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "ReadWriteFile")
static FString ReadStringFromFile(FString FilePath, bool& bOutSuccess, FString& OutInfoMessage);
UFUNCTION(BlueprintCallable, Category = "ReadWriteFile")
static void WriteStringToFile(FString FilePath, FString String, bool& bOutSuccess, FString& OutInfoMessage);
UFUNCTION(BlueprintCallable, Category = "ReadWriteFile")
static void AppendStringToFile(FString FilePath, FString String, bool& bOutSuccess, FString& OutInfoMessage);
};
.cpp content:
#include "RWtextFile.h"
#include "HAL/PlatformFileManager.h" // Core
#include "Misc/FileHelper.h" // Core
FString URWtextFile::ReadStringFromFile(FString FilePath, bool& bOutSuccess, FString& OutInfoMessage)
{
// Check if the file exists
if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*FilePath))
{
bOutSuccess = false;
OutInfoMessage = FString::Printf(TEXT("Read String From File Failed - File doesn't exist - '%s'"), *FilePath);
return "";
}
FString RetString = "";
// Try to read the file. Output goes in RetString
if (!FFileHelper::LoadFileToString(RetString, *FilePath))
{
bOutSuccess = false;
OutInfoMessage = FString::Printf(TEXT("Read String From File Failed - Was not able to read file. Is this a text file? - '%s'"), *FilePath);
return "";
}
bOutSuccess = true;
OutInfoMessage = FString::Printf(TEXT("Read String From File Succeeded - '%s'"), *FilePath);
return RetString;
}
void URWtextFile::WriteStringToFile(FString FilePath, FString String, bool& bOutSuccess, FString& OutInfoMessage)
{
// Try to write the string into the file
if (!FFileHelper::SaveStringToFile(String, *FilePath))
{
bOutSuccess = false;
OutInfoMessage = FString::Printf(TEXT("Write String To File Failed - Was not able to write to file. Is your file read only? Is the path valid? - '%s'"), *FilePath);
return;
}
bOutSuccess = true;
OutInfoMessage = FString::Printf(TEXT("Write String To File Succeeded - '%s'"), *FilePath);
}
void URWtextFile::AppendStringToFile(FString FilePath, FString String, bool& bOutSuccess, FString& OutInfoMessage)
{
// Try to write the string into the file
if (!FFileHelper::SaveStringToFile(String, *FilePath, FFileHelper::EEncodingOptions::AutoDetect, &IFileManager::Get(), EFileWrite::FILEWRITE_Append) )
{
bOutSuccess = false;
OutInfoMessage = FString::Printf(TEXT("Write String To File Failed - Was not able to write to file. Is your file read only? Is the path valid? - '%s'"), *FilePath);
return;
}
bOutSuccess = true;
OutInfoMessage = FString::Printf(TEXT("Write String To File Succeeded - '%s'"), *FilePath);
}