I have managed to write to a text file but I want to be able to add more data to the text file without overwriting the current contents of the text file. So in other words, I want to write to the end of the file each time so it doesn’t erase current content. The following code is what I have done so far but it overwrites every time and writes to the beginning, I am not sure how to get it write to the end. Based on this code, the current content in the textfile looks like:
June
July
August
September
I want to be able to add the following for example
June
July
August
September
October
Does anyone know how to this? Thanks!
-(void) writeToTextFile{
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = @"June\nJuly\nAugust\nSeptember";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
}
Use this method to write at End of file: