Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append string in local resource txt file for iOS sdk

I have been trying to append strings to a local resource file but I am having trouble finding a solution. I am trying to create a log file for all the function call in my application so if it crashes I can see which function it stopped on.

I have created a log.rtf file, but am not able to write in this file. Can someone please help me append a string to this file without having to overwrite the entire thing?

like image 508
Mobile App Dev Avatar asked Aug 12 '11 14:08

Mobile App Dev


2 Answers

I have use following code for the above problem.

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *logPath = [[NSString alloc] initWithFormat:@"%@",[documentsDir stringByAppendingPathComponent:@"log.rtf"]];
NSFileHandle *fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:logPath];
[fileHandler seekToEndOfFile];
[fileHandler writeData:[text dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandler closeFile];
like image 50
Mobile App Dev Avatar answered Oct 26 '22 06:10

Mobile App Dev


This way you can do this..

+ (void)WriteLogWithString:(NSString *)log
{

        if(log != nil){

            NSString *locationFilePath = [self getLogFilePath];//access the path of file

            FILE *fp = fopen([locationFilePath UTF8String], "a");

            fprintf(fp,"%s\n", [log UTF8String]);

            fclose(fp);
        }

}
like image 32
SachinVsSachin Avatar answered Oct 26 '22 05:10

SachinVsSachin