读文件:
IPlatformFile& file =FPlatformFileManager::Get().GetPlatformFile();
//获取游戏路径
FString gameRootDir =FPaths::ConvertRelativePathToFull(FPaths::RootDir());
FString absolutePath =gameRootDir + TEXT(“config.ini”);
FString fileContent;
//读取文件内容到FString
if(FFileHelper::LoadFileToString(fileContent, *absolutePath))
{
//判断字符串是否是数字形式
if(fileContent.IsNumeric())
{
//字符串转浮点数
speed= FCString::Atof(*fileContent);
returntrue;
}
}
写文件:
IPlatformFile& file =FPlatformFileManager::Get().GetPlatformFile();
FString gameRootDir =FPaths::ConvertRelativePathToFull(FPaths::RootDir());
m_statisticsDir = gameRootDir + “Statistic/”;
FString macAddr =FGenericPlatformMisc::GetMacAddressString();
FString absolutePath = m_statisticsDir + macAddr +TEXT(“.log”);
if (!file.DirectoryExists(*m_statisticsDir))
{
file.CreateDirectory(*m_statisticsDir);
}
//创建文件
IFileHandle* m_filehandle;
m_filehandle = file.OpenWrite(*absolutePath, true);
//获取时间
FString timestamp = FDateTime::Now().ToString();
const static TCHAR* endline = TEXT(“\r\n”);
const static TCHAR* space = TEXT(” “);
//写文件
//字符串编码格式转换
m_filehandle->Write((constuint8*)TCHAR_TO_ANSI(*timestamp),FCStringAnsi::Strlen(TCHAR_TO_ANSI(*timestamp)));
m_filehandle->Write((constuint8*)TCHAR_TO_ANSI(space), FCStringAnsi::Strlen(TCHAR_TO_ANSI(space)));
m_filehandle->Write((constuint8*)TCHAR_TO_ANSI(log), FCStringAnsi::Strlen(TCHAR_TO_ANSI(log)));
m_filehandle->Write((constuint8*)TCHAR_TO_ANSI(endline), FCStringAnsi::Strlen(TCHAR_TO_ANSI(endline)));
目录判断
IPlatformFile& file =FPlatformFileManager::Get().GetPlatformFile();
FString gameRootDir =FPaths::ConvertRelativePathToFull(FPaths::RootDir());
FString licenceDir = gameRootDir+ “licence/”;
FString macAddr =FString(machineID).Right(12);//FGenericPlatformMisc::GetMacAddressString();
FString absolutePath = licenceDir+ macAddr + TEXT(“.id”);
//make sure the dir exist
//判断目录
if(!file.DirectoryExists(*licenceDir))
{
//创建目录
file.CreateDirectory(*licenceDir);
}