配置文件路径如下:
按数据格式,把每项数据读取存放到数组,实现如下:
#include "Misc/FileHelper.h"//
USTRUCT(BlueprintType)
struct FAxisInfo
{
GENERATED_USTRUCT_BODY()
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MotoShakeDll")
int32 axis1;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MotoShakeDll")
int32 axis2;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MotoShakeDll")
int32 axis3;
FAxisInfo()
{
axis1 = 0;
axis2 = 0;
axis3 = 0;
}
};
void XXX::InitDataFile()
{
FString IniFilePath = FPaths::Combine(*FPaths::ProjectPluginsDir(), TEXT("AxisData.ini"));
FString Text;
AxisInfoArray.Empty();//清空之前的数据
if (FFileHelper::LoadFileToString(Text, *IniFilePath))
{
TArray
FString LeftS;
FString RightS;
FString DataX, DataY, DataZ;
FAxisInfo CurInfo;
//把换行回车符 替换成==,使用这个==切割
Text.ReplaceCharInline('\n', '=');
Text.ReplaceCharInline('\r', '=');
RightS = Text;
while (RightS.Split("==", &LeftS, &RightS))
{
//StrGroupArray.Append(const_cast
StrGroupArray.Add(LeftS);//每项都是 0 0 0 等格式
}
UE_LOG(LogTemp, Warning, TEXT("InitDataFile: StrGroupArray=%d"), StrGroupArray.Num());
for (int32 i = 0; i < StrGroupArray.Num(); i++)
{
//每项都是 0 0 0 等格式 每一项数字读取处理,两次切割每项字符串
FString TemStr = StrGroupArray[i];
TemStr = TemStr.Trim();//TemStr.TrimStartAndEndInline();
if (TemStr.IsEmpty())//空行去掉
continue;
StrGroupArray[i].TrimStartInline();//去掉第一个字符前空格 //TemStr.TrimStartAndEndInline();
if (StrGroupArray[i].Split(" ", &LeftS, &RightS))
{
DataX = LeftS;
RightS.TrimStartInline();//去掉第一个字符前空格 //TemStr.TrimStartAndEndInline();
if(RightS.Split(" ", &LeftS, &RightS))
{
DataY = LeftS;
DataZ = RightS;
}
else
{
continue;
}
}
else
{
continue;
}
//添加到自定义结构体数字
CurInfo.axis1 = FCString::Atoi(*(DataX.Trim()));
CurInfo.axis2 = FCString::Atoi(*(DataY.Trim()));
CurInfo.axis3 = FCString::Atoi(*(DataZ.Trim()));
AxisInfoArray.Add(CurInfo);//AxisInfoArray.Append(const_cast
}
UE_LOG(LogTemp, Warning, TEXT("InitDataFile:AxisInfoArray= %d"), AxisInfoArray.Num());
}
else
{
UE_LOG(LogTemp, Warning, TEXT("InitDataFile: Null "));
}
}