Creating a text file in C++

Hello! I can’t seem to find a way to create a new .txt file anywhere in the documentation. I can find functions for deleting files, moving files, reading and writing to them, but not creating them. if someone could point me in the right direction I would really appreciate it. thanks in advance :slight_smile:

I found this & this

HTH

hm… I have been both of these links before. CreateDirectory seems to create a folder, not a file.

https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Misc/FFileHelper/index.html

SaveStringToFile

the SaveStringToFIle() will create a new file if one does not already exist with that name?

You can use one of the following flags as the last argument to SaveStringToFile() to control how the file should be written:



FILEWRITE_None
FILEWRITE_NoFail
FILEWRITE_NoReplaceExisting
FILEWRITE_EvenIfReadOnly
FILEWRITE_Append
FILEWRITE_AllowRead


By default SaveStringToFile() will create a new file if it does not exist, and if it does already exist then it will be overwritten.

1 Like

This code is copied directly from my project and is working. It will create a file in your save folder called MessageLog.txt and will append strings to it.
First add these includes:
#include “FileHelper.h”
#include “Paths.h”

Then tweak this to your liking:
FString FilePath = FPaths::ConvertRelativePathToFull(FPaths::GameSavedDir()) + TEXT(“/MessageLog.txt”);
FString FileContent = TEXT("This is a line of text to put in the file.
");
FFileHelper::SaveStringToFile(FileContent, *FilePath, FFileHelper::EEncodingOptions::AutoDetect, &IFileManager::Get(), EFileWrite::FILEWRITE_Append);

thank you all very much! all of this knowledge helped me to fix the problem. have a great day!