How to write a line to a text file without deleting / replacing any word in the source file?

In Cocoa, how can I write a string to a text file without replacing the contents of the file, such as writing at the end of the file?

For example, the following code:

BOOL written = [data writeToFile:[path stringByAppendingPathComponent:@"conf.txt"] options:NSAtomicWrite error:&error];

The data (string) was written to a text file, however it replaced the original contents of the file.

Any suggestions?

+3
source share
1 answer

Use NSFileHandle.

First call -[NSFileHandle seekToEndOfFile]to find the end of the file.

Then use -[NSFileHandle writeData:](instead -[NSData writeToFile:]) to add your data to the end of the file.

+7
source

Source: https://habr.com/ru/post/1702801/


All Articles