I am new to iPhone programming. I want to read the content of a text file located in a subfolder of the Resource folder.
The Resource folder structure is the following:
Resource
- Folder1---->Data.txt
- Folder2---->Data.txt
- Folder3---->Folder1---->Data.txt
There are multiple files named "Data.txt", so how can I access the files in each folder? I know how to read the text file, but if the Resource structure is similar to the above structure then how can I get the path?
For example, if I want to access the "Data.txt" file from Folder3, how can I get the file path?
Please suggest.
This gives you the path to your bundle. From there on, you can navigate your folder structure.
Shirkrin's answer and PeyloW's answer above were both useful, and I managed to use
pathForResource:ofType:inDirectory:
to access files with the same name in different folders in my app bundle.I also found an alternative solution here that suited my requirements slightly better, so I thought I'd share it. In particular, see this link.
For example, say I have the following Folder References (blue icons, Groups are yellow):
Then I can access the image files like this:
As a side note, the
pathForResource:ofType:inDirectory:
equivalent looks like this:To continue psychotiks answer a full example would look like this:
Notice that you can use -pathForResource:ofType:inDirectory to access ressources in sub directories.
Your "resource folder" is actually the contents of your main bundle, also know as the application bundle. You use
pathForResource:ofType:
orpathForResource:ofType:inDirectory:
to get the full path for a resource.Loading the contents of a file as a string is done with the
stringWithContentsOfFile:encoding:error:
method for an autoreleased string of withinitWithContentsOfFile:encoding:error:
if you want a retained string.This is almost the same answer as given by Shirkrin previously, but with the slight difference that it works on target. This is because
initWithContentsOfFile:
is deprecated on Mac OS X, and not available at all iPhone OS.