issac
issac

Reputation: 1501

How to write a string to a text file without deleting/replacing any word in the original file?

In Cocoa, how can I write a string to a text file without replacing the contents of the file, like 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 the text file however it replaced the original contents of the file.

Any suggestions?

Upvotes: 3

Views: 1699

Answers (1)

colithium
colithium

Reputation: 10327

Use an NSFileHandle.

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

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

Upvotes: 7

Related Questions