Reputation: 1501
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
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