Skip to main content
gamedev.net
🔒 Locked

XCode fileIO

Started by Aphyxia 3 replies 1.2k views
Original Post
Aphyxia
Aphyxia
I am trying to figure out how to open a fstream with a relative filepath for an iphone game i'm making, I know there is a phones section but i'm pretty sure this problem is compiler specific not iphone specific. for example in visual studio it goes from the project file so if i wanted to open a file "openme.txt" in the path that follows c:/(...)/solution folder/the Project Folder/subfolder1/subfolder2/openme.txt i would do ifstream fin("subfolder1/subfolder2/openme.txt"); The only way i can figure out in xcode is give it the absolute path(eww). or NSString* path = [[NSBundle mainBundle] pathForResource: @"openme" ofType: @"txt"]; ifstream fin([path cStringUsingEncoding:1]); I have 3 major problems with that method. 1. that fills path out with a fullpath name still, not a relative one (not a big deal yet but seems ugly) 2. for that function to work it requires the file to be included in the project, so i can't just make a new image and script file with that file name in it to create a new level, I have to actually rebuild the project with a new image... makes 'expansion packs' difficult. 3. I realize its not what apple "prefers" but i'm trying to keep the 'objective-C' out of the actual game code and just in the wrappers so that the game itself is as portable as can be. Anyway, i suppose this could have been shorter but i wanted to explain why I'd prefer not to use that approch. Thanks.
Zakwayda
Zakwayda
Since no one's answered, I'll go ahead and post a reply. I don't know how things work on the iPhone, but on a Mac running OS X, the method I'm familiar with for changing the current working directory involves using Objective C (like in your example).

My own Mac apps are built around SDL, so this just involves making some changes to SDL_main (which isn't too difficult).

If you get really stuck on this (and don't get any additional replies here), you might try posting over on idevgames.com.
the_edd
the_edd
There should be a setting in the IDE to set the working directory for the active executable. It's probably in a slightly different place depending on the version of XCode you've got. But I'd start by right-clicking the executable in the tree of stuff on the left.

EDIT: http://developer.apple.com/documentation/developertools/conceptual/XcodeProjectManagement/080-Defining_Executable_Environments/chapter_9_section_5.html
Aphyxia
Aphyxia
Thanks guys, i'll look into those... I'm also going to write an archiver so i can hide the indivdual files from the end user. then i only need to open the one file and the files in the archive I can change/link to however i want.
swiftcoder
swiftcoder
Either method should work for testing on your Mac, but remember that you can't read/write arbitrary files on the iPhone - the security features only let you read files stored inside your application bundle anyway.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.