Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fopen() on the iPhone?

The iPhone SDK docs claim fopen() is a supported method of file access but I am unable to get it to return a FILE handle. I am accessing a directory which is included in my project. I have tried fopen "filename","dir/filename","./filename","./dir/filename","/dir/filename" all returning with a null pointer. Some people report using it with no issue so I am sure it is something simple!

like image 484
Nick Van Brunt Avatar asked Nov 25 '08 18:11

Nick Van Brunt


People also ask

How do I open my files on iPhone?

Browse and open files and foldersTap Browse at the bottom of the screen, then tap an item on the Browse screen. If you don't see the Browse screen, tap Browse again. To view recently opened files, tap Recents at the bottom of the screen. To open a file, location, or folder, tap it.

How do I access the internal storage on my iPhone?

Go to Settings > General > [Device] Storage. You might see a list of recommendations for optimizing your device's storage, followed by a list of installed apps and the amount of storage each one uses. Tap an app's name for more information about its storage. Cached data and temporary data might not be counted as usage.

Does iPhone have file manager?

Apple's Files app lets you view and access files stored on online services, such as iCloud Drive, Box, Dropbox, Google Drive, and Microsoft OneDrive, all in one place. You can also view your files directly on your iPhone or iPad and run a variety of commands on them.


2 Answers

Just to be clear to open a file "some.txt"...

NSString * path = [[NSBundle mainBundle] pathForResource:  @"some" ofType: @"txt"];
FILE *f = fopen([path cStringUsingEncoding:1],"r");
if (f == NULL) NSLog([path stringByAppendingString:@" not found"]);
like image 191
Nick Van Brunt Avatar answered Oct 23 '22 11:10

Nick Van Brunt


if you're trying to access a file within your application bundle, you need to get the full path to it: [[NSBundle mainBundle] pathForResource: FILENAME ofType: FILEEXTENSION]

This returns an NSString, which you can pull a UTF8String out of and pass to fopen.

like image 42
Ben Gottlieb Avatar answered Oct 23 '22 11:10

Ben Gottlieb