我想將我的日誌字符串記錄到文件中。iPhone:記錄到文件
我採取了以下方法:
void QuietLog (NSString *format, ...) {
if (format == nil) {
printf("nil\n");
return;
}
va_list argList;
va_start(argList, format);
NSMutableString *s = [[NSMutableString alloc] initWithFormat:format
arguments:argList];
[s replaceOccurrencesOfString: @"%%"
withString: @"%%%%"
options: 0
range: NSMakeRange(0, [s length])];
#ifdef LOGTOFILE_MODE
NSData *dataToWrite = [s dataUsingEncoding: NSUTF8StringEncoding];
[dataToWrite writeToFile: Log_FilePath atomically: YES];
#else
printf("%s\n", [s UTF8String]);
#endif
[s release];
va_end(argList);
}
的將writeToFile方法替換所有在一個文件中最後一個字符串之前的數據。但我想保留以前添加的數據並添加一個新的字符串。
writeToFile有什麼問題?它爲什麼會覆蓋所有現有的數據?如何添加新字符串而不是覆蓋舊字符串?
謝謝。
更新我的回答是更有效的。論壇帖子我參考了你需要的所有信息。 – 2009-07-01 22:15:20