UE4文件读写(.txt文本文档)

UE4文件读写(.txt文本文档)

————————————————————————————————

一、创建C++类继承BlueprintFunctionLibrary

在这里插入图片描述
在这里插入图片描述

二、用到的函数

读取文件

bool FFileHelper::LoadFileToString( FString& Result, const TCHAR* Filename, EHashOptions VerifyFlags, uint32 ReadFlags)

写入文件

bool FFileHelper::SaveStringToFile( const FString& String, const TCHAR* Filename,  EEncodingOptions EncodingOptions, IFileManager* FileManager /*= &IFileManager::Get()*/, uint32 WriteFlags )

获得项目Content文件路径

FString FPaths::ProjectContentDir()
{
	return FPaths::ProjectDir() + TEXT("Content/");
}

三、代码

在vs中编译可能会报错,在工程中编译通过就ok

.h文件

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "TxtReadAndWrite.generated.h"

/**
 * 
 */
UCLASS()
class READANDWRITETXT_API UTxtReadAndWrite : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
public:

	UFUNCTION(BlueprintCallable, Category = "HandleTxt")
		static FString ReadTxt(FString path);

	UFUNCTION(BlueprintCallable, Category = "HandleTxt")
		static bool WriteTxt(FString saveString, FString path);
	
};

.CPP文件

#include "TxtReadAndWrite.h"
#include"Runtime/Core/Public/HAL/PlatformFilemanager.h"
#include"Runtime/Core/Public/Misc/FileHelper.h"
#include"Runtime/Core/Public/Misc/Paths.h"


FString UTxtReadAndWrite::ReadTxt(FString path) 
{
	FString resultString;

	FFileHelper::LoadFileToString(resultString, *(FPaths::ProjectContentDir() + path));

	return resultString;
}


bool UTxtReadAndWrite::WriteTxt(FString saveString, FString path)
{
	bool result;
	result = FFileHelper::SaveStringToFile(saveString, *(FPaths::ProjectContentDir() + path));
	return result;
}

四、新建txt文件

放到项目Content文件夹下面,打包后也要将.txt文件手动移动到包的Content文件夹下。
放到

五、在蓝图中使用

在这里插入图片描述
在这里插入图片描述
写入和读取都可以了。

  • 13
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值