我使用writeToFile
创建了input.txt文件,但是在项目路径中没有此文件我不知道如何找到它。
NSString* str2 = [NSString stringWithUTF8String:cstr];
[str2 writeToFile:@"input.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString* str3 = [NSString stringWithContentsOfFile:@"input.txt" encoding:NSUTF8StringEncoding error:nil];
答案 0 :(得分:3)
您需要将app Documents文件夹的路径添加到您的文件中:
//get path to Documents
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//add path to you file name
NSString *fileWithPath = [documentPath stringByAppendingPathComponent:@"input.txt"];
//then write
[str2 writeToFile:fileWithPath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL];
然后你也可以使用fileWithPath读取它:
NSString* str3 = [NSString stringWithContentsOfFile:fileWithPath encoding:NSUTF8StringEncoding error:nil];