x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Append to text file instead of overwrite whats there?

Hello! I have been following Rama's tutorial on how to write to a text file from UE. ( link ) It is a very helpful walkthrough, but I am wondering if there is a way to append to the text file instead of writing over what is already there. I have been writing to the file every 10 seconds and make sure to print my the RunTime so I can see that it is indeed overwriting what is currently there. (Spoiler: it is)

The code below has been writing a single line to the file, overwriting what is already there.

 FFileHelper::SaveStringToFile(SaveText, * SaveDirectory);

The code below is able to write the string twice on the same line, but I was reading that I have to delete the file handle before quitting my program. Is there an "OnQuit" function available where I can delete it?

 IPlatformFile& file = FPlatformFileManager::Get().GetPlatformFile();
 IFileHandle* handle = file.OpenWrite(*directory);
 if (handle)
 {
     handle->Write((const uint8*)TCHAR_TO_ANSI(*string), string.Len());
     handle->Write((const uint8*)TCHAR_TO_ANSI(*string), string.Len());
     delete handle;
 }

Also, my string has a \n in it but doesn't seem to add a new line.. is there something else I have to add?

All help is greatly appreciated!

Product Version: UE 4.7
Tags:
more ▼

asked Jun 30 '15 at 01:15 AM in C++ Programming

avatar image

G8S
16 1 1 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Hello, FFileHelper::SaveStringToFile() has 5 arguments:

  1. const FString & String,

  2. const TCHAR* Filename,

  3. EEncodingOptions::Type EncodingOptions,

  4. IFileManager * FileManager,

  5. uint32 WriteFlags

Last three of them are not obligatory, but you can use them if you need more custom setup. The last parameter is WriteFlags, there are several of them defined in FileManager:

  • enum EFileWrite

  • { FILEWRITE_None = 0x00,

  • FILEWRITE_NoFail = 0x01,

  • FILEWRITE_NoReplaceExisting = 0x02,

  • FILEWRITE_EvenIfReadOnly = 0x04,

  • FILEWRITE_Append = 0x08,

  • FILEWRITE_AllowRead = 0x10 };

To append you can use FILEWRITE_Append. Here is the example of call for append: **FFileHelper::SaveStringToFile(StringToSave, PathToFile, FFileHelper::EEncodingOptions::AutoDetect, &IFileManager::Get(), EFileWrite::FILEWRITE_Append)*

more ▼

answered Apr 20 '17 at 08:47 AM

avatar image

mchernykh
58 2 5 9

avatar image LDOGLONDON Dec 12 '18 at 10:35 PM

I'm doing this, but when I append a string it doesn't add a newline. Is there anything I can do to add a newline?

avatar image Jake_Schwartz Dec 13 '18 at 01:50 AM

Thanks mcbernykh!

LDOGLONDON : try adding an escape return character at the beginning or end of your string depending on what you need. I found that "\n" works when opening as a .csv but not as a text file but "\r\n" seems to work correctly.

So for example if you did:

"First line \r\n Second line"

It would look like :

First line

Second line

and if you are appending on to an existing file and seeing this:

Original line.First line

Then try: "\r\n First line"

and it should come in as :

Original line.

First line.

avatar image LDOGLONDON Dec 13 '18 at 02:00 AM

Thanks, yeah I eventually got it to work doing that. I was being thrown off because I was deleting everything in the file I was writing to, then testing it so it would write to that file again. And for some reason I got like Chinese letters, but all I had to do to fix the problem was delete the file and let the program generate a new one. Then the newline and everything worked fine.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question