Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write in append mode for text file

Tags:

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];

}
like image 491
SOF Avatar asked Jan 24 '11 08:01

SOF


People also ask

How do you write in append mode?

In order to append a new line your existing file, you need to open the file in append mode , by setting "a" or "ab" as the mode. When you open with "a" mode , the write position will always be at the end of the file (an append).

How do I append a text file?

To append to a text fileUse the WriteAllText method, specifying the target file and string to be appended and setting the append parameter to True . This example writes the string "This is a test string." to the file named Testfile. txt .

How do you add append a file?

You can use cat with redirection to append a file to another file. You do this by using the append redirection symbol, ``>>''. To append one file to the end of another, type cat, the file you want to append, then >>, then the file you want to append to, and press <Enter>.

Which function is used to append text to a text file?

PHP Append to File - fwrite() The PHP fwrite() function is used to write and append data into file. fwrite($fp, ' this is additional text ');


2 Answers

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]];
like image 184
KingofBliss Avatar answered Sep 19 '22 03:09

KingofBliss


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]];
}
like image 30
Samyukt Shah Avatar answered Sep 22 '22 03:09

Samyukt Shah