Mac 上fopen总返回NULL

全局,相对路径都不行,

在沙盒中获取也不行

  1. //在沙盒中获取Documents的完整路径  
  2. NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
  3. //得到path下test文件的路径  
  4. NSString * filePath = [path stringByAppendingPathComponent:@"test"];  
  5. //判断test文件是否存在  
  6. NSFileManager * fm = [NSFileManager defaultManager];  
  7. if ([fm fileExistsAtPath:filePath]) {  
  8.     NSLog(@"test文件存在");  
  9. }else{  
  10.     NSLog(@"test文件不存在");  
  11. }  
  12.   

使用bundle

NSBundle *mainBundle = [NSBundle mainBundle];

 NSString *imagePath = [mainBundle pathForResource:@"abc" ofType:@"png"];
在build PHASE 的copy bundle resource中将要播放的文件添加
然后在fopen([文件指针,utf8...],"rb")

2 发现写文件的时候,bundle路径打开文件又出错了:
因此写文件使用NShomedirectory();
nsstring *path = nshomedirectory();
path = [path stringbyappendingstring:@"文件名"];
file *FPATH = fopen([path utf8string] ,"wb");
到时候去 finder下 commend shift g 然后~/library 下找到保存的写文件
原文地址:https://www.cnblogs.com/8335IT/p/8436236.html