Access File created in UE4 using ioStream in Android

Hi All,

   I was trying to integrate an FIT SDK into game I was working on. I got SDK of FIT as C++ and I wanted to create a file using fstream in Unreal. But, it was failed.

So, I created a file in Unreal using

FFileHelper::SaveStringToFile(TEXT(""), *path);

Then, I tried to access the file using ios::fstream

std::fstream InFile;
InFile.open(TCHAR_TO_UTF8(*path),  std::fstream::out);

But, I can’t able to access it. As file has been created successfully in the UE4.
But, when I tried to access the file from the file location after packaging. (Android ) it was not working as expected.

if (InFile.fail())
{
     UE_LOG(LogTemp, Warning, TEXT("FAILED"));
}
else
{
     UE_LOG(LogTemp, Warning, TEXT("SUCCESS"));
}

Everytime, it was going into the IF condition. Which means, fstream can’t able to access the file or what is the problem here.

Can anyone help me to understand here and also guide me through right direction.

Update:
In Editor, it was working fine. When I make build and run it on the device (Android) that’s when I was not able to access the file.

Thanks

Hi,

Can you please post your path ? because I think that the problem is there.
Also the method FFileHelper::SaveStringToFile returns a bool, you can check whether you function returns true or false for the file creation.

So I tested your code by changing the path, using the Unreal Engine class FPaths to get the project directory and create the file using the same method as you and i get a success for the IF condition.

This is the code I used :

   FString path = FPaths::ProjectDir();
	path += "Content/TextFile.txt";

	
	bool bTest = FFileHelper::SaveStringToFile(TEXT("my awesome file"), *path);
	UE_LOG(LogTemp, Error, TEXT(" bTest : %d"), bTest);

	std::fstream InFile;
	InFile.open(TCHAR_TO_UTF8(*path), std::fstream::out);

	if (InFile.fail())
	{
		UE_LOG(LogTemp, Warning, TEXT("FAILED"));
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("SUCCESS"));
	}

So my file will be created in the Content folder of the project and I see that my text file is successfully created with the test in it.

Also the code above is in my Actor BeginPlay().

Can you post more about your code to better understand what’s going wrong ?

Hope this helps :slight_smile:

Thanks Hakaishin0895,

Sorry, I forgot to mention, I was facing this issue only on device(Android). I was trying to access the file. It was created in Android packaged build. But, when I try to access via fstream It was failing.