how to write in append mode for text file

Solution 1:

Remove the following line of code

[savedString writeToFile:documentTXTPath atomically:YES];

And remaining code are all fine. Anyway check my code also,

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
    NSString *savedString = textview.text;
    NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:documentTXTPath];
    [myHandle seekToEndOfFile];
    [myHandle writeData:[savedString dataUsingEncoding:NSUTF8StringEncoding]];

Solution 2:

Try this it works great

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
NSString *savedString = textview.text;

If file Does not exist create it and write to file or else its already created append to end of the file

NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:documentTXTPath])
{
  [savedString writeToFile:documentTXTPath atomically:YES];
}
else
{
  NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:documentTXTPath];
  [myHandle seekToEndOfFile];
  [myHandle writeData:[savedString dataUsingEncoding:NSUTF8StringEncoding]];
}