Writing to a new line of a file in Objective-C

Posted by Kulpreet on Stack Overflow See other posts from Stack Overflow or by Kulpreet
Published on 2010-04-15T16:00:26Z Indexed on 2010/04/15 16:03 UTC
Read the original article Hit count: 461

For some strange reason, the \n and \r escape sequences do not seem to be working in my code. I want to write each NSString on a new line of the file, but it just appends it the last string on one line. Here's the code:

for (Entry *entry in self.entries) {
    NSString *path = @"/Users/me/File.txt";
    NSString *string = (@"%@\r\n", [entry thisEntry]);
    NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:path];
    [fh seekToEndOfFile];
    [fh writeData:[string dataUsingEncoding:NSUnicodeStringEncoding]];
    [fh closeFile];
}

Am I doing something wrong? Forgive me as I am new to Objective-C.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about nsfilehandle