0

So whenever I write to a file in my iPad app, it writes over the first line instead of writing to the next line down, so my file winds up only being a single line long and displays only the very last line of text.

This is the code I'm using to write to a file:

[myString writeToFile:filePath 
           atomically:YES 
             encoding:NSASCIIStringEncoding 
                error:nil];

I also tried making it so "myString" ended in the character "\n" to see if that would make it skip a line but it didn't.

So if anyone knows what's wrong it'd really help me out.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user1855952
  • 1,515
  • 5
  • 26
  • 55
  • writeToFile always overwrite the existing file. Try the answers found here: http://stackoverflow.com/questions/4779877/how-to-write-in-append-mode-for-text-file – Valent Richie Jun 01 '13 at 12:47

1 Answers1

1
NSMutableString* content = [NSMutableString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:&error];

[content appendString:yourNewString];
[content writeToFile:path atomically:YES];

Hope this helps..

Anupdas
  • 10,211
  • 2
  • 35
  • 60
lakshmen
  • 28,346
  • 66
  • 178
  • 276