ini*_*333 15
由于问题是明确要求C/C++和NOT Obj-C,我正在添加这个答案,我在这里看到并可能帮助某人:
char buffer[256];
//HOME is the home directory of your application
//points to the root of your sandbox
strcpy(buffer,getenv("HOME"));
//concatenating the path string returned from HOME
strcat(buffer,"/Documents/myfile.txt");
//Creates an empty file for writing
FILE *f = fopen(buffer,"w");
//Writing something
fprintf(f, "%s %s %d", "Hello", "World", 2016);
//Closing the file
fclose(f);
Run Code Online (Sandbox Code Playgroud)
使用此C代码:
buffer = "/private/var/mobile/Containers/Data/Application/6DA355FB-CA4B-4627-A23C-B1B36980947A/Documents/myfile.txt"
Run Code Online (Sandbox Code Playgroud)
而Obj-C版本:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
正在回归
documentsDirectory = /var/mobile/Containers/Data/Application/6DA355FB-CA4B-4627-A23C-B1B36980947A/Documents
Run Code Online (Sandbox Code Playgroud)
仅供参考,如果您注意到"私人"的小差异,我写信给两个地方,他们碰巧是一样的.
是.
使用NSFileHandle让读文件/写访问,并NSFileManager创建目录,并列出它们的内容和做其他类型的文件系统访问.
请记住,每个App都是沙盒,只能写入自己指定的目录.您可以使用以下代码获取这些目录的路径:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
Run Code Online (Sandbox Code Playgroud)
但是,您无法访问其他应用程序的文件.
iTunes会自动备份这些目录,因此如果用户进行设备还原,它们会自动恢复.另请注意,您不应以文件浏览器或"保存对话框"等形式将文件系统"公开"给App用户.