0

I am trying to call a method that prints to a txt file. But then when I call the method again it will just overwrite what was in the text file with my new text. I want to file to just keep adding what is printed.

-(void) print:(NSString *)name
{
NSURL *url = [NSURL fileURLWithPath:@"/here/file.txt"];
NSString *fileContent = [NSString stringWithFormat"@"%@\n", name];
[fileContent writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

When I call this method I am calling it by doing:

[name print: person1.name];
[name print: person2.name];

Everytime I try to print the next name it just replaces the name in the text file. I am unsure how to concantenate and continue adding the names?

Thank you

I have tried this:

NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:@"/here/file.txt"];
NSString *textToWrite = @"hello";
[fileHandle seekToEndOfFile];
[fileHandle writeData:[textToWrite dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];

Nothing is printing.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132

2 Answers2

0

Maybe something like this is better (I tried to integrate your solution)

-(void) print:(NSString *)name
{
    NSString* path = [[NSBundle mainBundle] pathForResource:@"file.txt" 
                                                     ofType:@"txt"];
    NSString* content = [NSString stringWithContentsOfFile:path
                                                  encoding:NSUTF8StringEncoding
                                                     error:NULL];
    NSString *fileContent = [NSString stringWithFormat"%@\n%@", content, name];
    NSURL *url = [NSURL fileURLWithPath:path];
    [fileContent writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

even though I guess there could be more efficient ways.

Nicola Miotto
  • 3,647
  • 2
  • 29
  • 43
0
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:aPath];
if (!fileHandle){
    NSLog(@"Error: File does not exist!");
} else {
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:[textToWrite dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandle closeFile];
}

Look here: https://stackoverflow.com/a/11106678/2115477

Community
  • 1
  • 1
lootsch
  • 1,867
  • 13
  • 21
  • Updated my first post. – user3166940 Mar 07 '14 at 14:58
  • Note that if you use NSFileHandle, you must make sure that the file exists before hand. fileHandleForWritingAtPath will return nil if no file exists at the path. See the NSFileHandle class reference. Also from http://stackoverflow.com/a/11106678/2115477 You could also use the second approach: `NSString *contents = [NSString stringWithContentsOfFile:filepath]; contents = [contents stringByAppendingString:textToWrite]; [contents writeToFile:filepath atomically:YES encoding: NSUnicodeStringEncoding error:&err];` – lootsch Mar 07 '14 at 15:07
  • File does exist in that exact location. – user3166940 Mar 07 '14 at 15:08
  • please debug and look if fileHandle is `nil` after `fileHandleWritingAtPath`. If it's `nil` there's no file to write at that path: [Reference](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsfilehandle_Class/Reference/Reference.html#//apple_ref/occ/clm/NSFileHandle/fileHandleForWritingAtPath:) – lootsch Mar 07 '14 at 15:16
  • I dont know how to do that – user3166940 Mar 07 '14 at 15:20
  • i added a if-else statement to my answer. – lootsch Mar 07 '14 at 15:25
  • I am new to this. i don't know what you are talking about. I know what a if else statement is but I don't know how to do what your wanting. The file IS there. I had it working with the exact same file directory but it just wasn't appending. Now i changed it to the above fileHandle method and its not working. I right click on the file.txt and view file information to copy the exact directory path. – user3166940 Mar 07 '14 at 15:26
  • Just tried it and nothing is printing to the console. – user3166940 Mar 07 '14 at 15:31
  • I got it to work. Some reason my program was being paused so I deactivated breakpoints and now it is working. Thank you for the help. – user3166940 Mar 07 '14 at 15:38