Can someone please take a look at my code and tell me what I am doing wrong? I am trying to get the hang of loading a .txt (or later .csv) file to an array, so it can then be used to populate items. In my test run, I only want to load to array and print to log. However, no matter where I put the file, or what I do, the ‘else’ statement below fires and ‘no text’ is shown. Can anyone see what I am doing wrong? Where does the file need to be to be accessed? Thanks in advance!!
//Array to store incoming text from file
TArray LevelInfo;
//Filepath
FString fileNameLvl = FString(“Files/LevelTest1.txt”);
//Checking file exists
if (FFileHelper::LoadFileToStringArray(LevelInfo, *fileNameLvl))
{
FString ToBePrinted = " ";
for (auto& Str : LevelInfo)
{
ToBePrinted += Str;
UE_LOG(LogTemp, Warning, TEXT("ToBePrinted"));
}
else//File could not be found?
{
UE_LOG(LogTemp, Warning, TEXT("No file"));
}
My guess would be that you need to specify a full path to a file, otherwise it’s probably looking for like Binaries\Win64\Files\LevelTest1.txt or something like that, whatever the CWD of hte process is.
Something like FString fileNameLvl = FPaths::ProjectContentDir() / TEXT(“Files/LevelTest1.txt”)
So I KNOW it can find it, but then when I run if (FPaths::FileExists(*fileNameLvl)) with the SAME variable, it returns negative. I have an If/else statement to check and it always returns else:
TArray LevelInfo;
//Theory from Unreal engine forum
FString fileNameLvl = FPaths::ProjectContentDir()/TEXT(“Dark/Source/Files/LevelTest1.txt”);
//Display full filename to log
UE_LOG(LogTemp, Warning, TEXT(“Text, %s”), *fileNameLvl);
//Checking file exists
if (FPaths::FileExists(*fileNameLvl))
{
UE_LOG(LogTemp, Warning, TEXT("it exists"));
if (FFileHelper::LoadFileToStringArray(LevelInfo, *fileNameLvl))
{
FString ToBePrinted = " ";
for (auto& Str : LevelInfo)
{
ToBePrinted += Str;
UE_LOG(LogTemp, Warning, TEXT("ToBePrinted"));
}
}
else//File could not be found?
{
UE_LOG(LogTemp, Warning, TEXT("No file"));
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Try again"));
}
Never mind I figured it out!! Thank you SO much for your help! I had to much being appended to the source directory. By appending this "Dark/Source/Files/LevelTest1.txt” I was actually directing it elsewhere. I moved the text file and it’s source folder “Files” under Content where project directory was looking and it found it!! Thanks again for your help!!!