1

File exists in iOS bundle directory. fopen() always return NULL on iOS device, but OK on iOS sumulator, codes as below:

NSString *path = [NSBundle.mainBundle pathForResource:@"yuv420p_640x360" ofType:@"yuv"];
BOOL exists = [NSFileManager.defaultManager fileExistsAtPath:path];
NSLog(@"%d", exists); //prints "1"
const char *path2 = [path UTF8String];
FILE *fp = fopen(path2, "rb+"); //fp is NULL

Environment: macOS 12.0.1, M1 MacBook Pro, iPadOS 15.0

Smeegol
  • 2,014
  • 4
  • 29
  • 44
  • The iOS execution environment is sandboxed, and you can not open a file in the bundle with "writing" mode, since the files in the bundle can not be modified (try with just "r" for the mode). – Christos Koninis Dec 13 '21 at 09:31
  • @ChristosKoninis Yeah, finally I just change "rb+" to "rb" and run OK. thanks – Smeegol Dec 13 '21 at 10:40
  • 1
    Since it helped I am moving my comment to an full answer for better visibility in case some one has the same problem to easily find the solution – Christos Koninis Dec 13 '21 at 10:51

2 Answers2

1

The iOS execution environment is sandboxed, and you can not open a file in the bundle with "writing" mode, since the files in the bundle can not be modified (try with just "r" for the mode).

Christos Koninis
  • 1,499
  • 1
  • 15
  • 28
1

You cannot open any file in iOS. Apps are sandboxed in iOS i.e. An app can access only its directory and cannot access(read/write) any file outside its directory.

Saurav_Sharma
  • 130
  • 1
  • 10