我只是想写入目标 C 中的 .txt 文件。这是代码:
BOOL success = [str writeToFile:@"tmp/cool.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
if(success)
{
NSLog(@"done writing!");
}
else
{
NSLog(@"writing failed: %@", [error localizedDescription]);
}
Run Code Online (Sandbox Code Playgroud)
此代码的输出是“文件夹cool.txt 不存在”。我不明白这一点,因为“.txt”会认为它是文件。
我究竟做错了什么?
我为你写了一个演示,假设你使用 iOS。
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp"]; //get tmp path
NSString *filePath = [path stringByAppendingPathComponent:@"cool.txt"];
NSString *str = @"hello world";
NSError *error;
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSString *str1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@", str1);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
608 次 |
最近记录: |