Skip navigation.
 
mlRe: Appending to file with writeToFile
FROM : Ken Thomases
DATE : Thu May 29 17:07:45 2008

On May 29, 2008, at 9:42 AM, Glover,David wrote:

> I'm using writeToFile to send strings to a log file at various 
> points in
> my program, however it doesn't append the strings, it just overwrites
> the previous one.  So the only thing in the log file is the very last
> string that was sent to it.


Yes.  The writeToFile:... methods mean "create a file with the 
receiver as its contents".  It isn't for incremental building of a 
file, but for wholesale creation of a file.


> I've searched around for a while and can't find any way to append the
> strings in my file.  I was wondering if anyone else had achieved this?
> Or if anybody knows how to do it?


Use NSFileHandle.  For example:

   NSFileHandler* fh = [NSFileHandle 
fileHandleForWritingAtPath:myOutputFilePath];

   // ...

   [fh writeData:[myString dataUsingEncoding:NSUTF8StringEncoding]];

Cheers,
Ken

Related mailsAuthorDate
mlAppending to file with writeToFile Glover,David May 29, 16:42
mlRe: Appending to file with writeToFile Jens Alfke May 29, 17:03
mlRe: Appending to file with writeToFile Ken Thomases May 29, 17:07
mlRE: Appending to file with writeToFile Glover,David May 29, 17:23