如何在iOS sdk的本地资源txt文件中附加字符串

发布于 2024-11-29 10:10:17 字数 156 浏览 0 评论 0原文

我一直在尝试将字符串附加到本地资源文件,但找不到解决方案。我正在尝试为应用程序中的所有函数调用创建一个日志文件,这样如果它崩溃了,我可以看到它停止在哪个函数上。

我创建了一个 log.rtf 文件,但无法在此文件中写入。有人可以帮我在这个文件中附加一个字符串,而不必覆盖整个文件吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱情眠于流年 2024-12-06 10:10:17

我已经使用以下代码来解决上述问题。

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];

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];
々眼睛长脚气 2024-12-06 10:10:17

这样你就可以做到这一点..

+ (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);
        }

}

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);
        }

}
冰雪梦之恋 2024-12-06 10:10:17
- (void)log:(NSString *)message {
    NSMutableString *string = [[NSMutableString alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/log.txt", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]]];
    if (!string) string = [[NSMutableString alloc] init];
    [string appendFormat:@"%@\r\n", message];
    [string writeToFile:[NSString stringWithFormat:@"%@/log.txt", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
- (void)log:(NSString *)message {
    NSMutableString *string = [[NSMutableString alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/log.txt", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]]];
    if (!string) string = [[NSMutableString alloc] init];
    [string appendFormat:@"%@\r\n", message];
    [string writeToFile:[NSString stringWithFormat:@"%@/log.txt", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文