22

My application navigation based. UItextView for notes UIViewController. I am writing data for text to file. Now i need to write in append mode , the below code i am trying but every time is writing to two time with same text data, and not appending if next text data to file.

- (void)saveText:(id)sender
{
    [self.textview resignFirstResponder];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
    NSString *savedString = textview.text;
    [savedString writeToFile:documentTXTPath atomically:YES];


    NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:documentTXTPath ];
    [myHandle seekToEndOfFile];
    [myHandle writeData:  [savedString dataUsingEncoding:NSUTF8StringEncoding]];
    [myHandle closeFile];

}
KingofBliss
  • 15,055
  • 6
  • 50
  • 72
SOF
  • 437
  • 2
  • 9
  • 19

3 Answers3

33

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]];
Brad The App Guy
  • 16,255
  • 2
  • 41
  • 60
KingofBliss
  • 15,055
  • 6
  • 50
  • 72
  • If remove [savedString writeToFile:documentTXTPath atomically:YES]; file is not create to write. – SOF Jan 24 '11 at 08:28
  • 2
    Once you have already file it will add append . first delete your text file in document folder and write to file and check , the file is there or not in document . because i am not able to see text file in my document folder. – SOF Jan 24 '11 at 10:04
  • stderr is writing for log , i tried as freopen([documentTXTPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stdout); it is working fine . text is appeding, but, if text is already there in UITEXTView it is writing one more time of same text. – SOF Jan 25 '11 at 06:01
  • @SOF when you're calling [writeToFile:documentTXTPath atomically:YES] you should set atomically to NO if you dont want to overwrite or "nuke" your current document. Check out the documentation on NSData's writeToFile:atomically: and writeToFile:options:error which might be helpful if you pass NSDataWritingWithoutOverwriting to the options parameter. – self.name Jan 31 '14 at 18:16
  • 1
    @self.name atomic is not the same as overwriting. Atomic ensures that file contents are written on disk before contents are being replaced. Usually this is achieved through auxiliary (temp) file that is later moved over destination. – pronebird Nov 23 '16 at 14:29
30

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]];
}
Samyukt Shah
  • 1,337
  • 12
  • 13
  • 2
    This is a better answer than KingOfBliss's one because it's crucial to create the file if it does not exist yet – Ariel Malka Dec 27 '17 at 16:08
-1

You can also do

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.text"];
NSString *content = [NSString stringWithContentsOfFile:documentTXTPath encoding:NSUTF8StringEncoding error:nil];
content = [content stringByAppendingString:data];

And again write contents to particular file path

[content writeToFile:documentTXTPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
user3575114
  • 993
  • 7
  • 13