iphone file issue
Do fread() and fwrite() not work on the iphone? I'm using this bit of code to try and save and load games, it compiles just fine, except for warning,"char *cpath = [p UTF8String];" which get a warning "initialized discards qualifiers from pointer target type". I do not get this warning when using the same code in a cocoa app though.
Code:
char* file_path(NSString* path)
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *p;
p = [bundle pathForResource:path
ofType:nil];
char *cpath = [p UTF8String];//<--------compiler warning here
return cpath;
}
void load_game(void)
{
FILE* filePtr = fopen( file_path(@"saved_game"), "rb" );
fread(&g_saved_game, sizeof(saved_game_t),
1, filePtr);
}
why would that be an issue?
It's only a problem if you try to change the value of the pointer returned by your function. The warning you're getting is just saying "Hey, this is a constant, but you're treating it like a variable!"
Return a const char* and the compiler won't complain:
Return a const char* and the compiler won't complain:
Code:
const char* file_path(NSString* path)
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *p;
p = [bundle pathForResource:path fType:nil];
return [p UTF8String];
}
Right, so then that wouldn't explain fread() and fwrite() not working.
fread/write should work fine, as long as your paths makes sense. You can't write to the app bundle on iPhone though (and shouldn't on Mac OS X either) so pathForResource won't do you any good for saving. Use NSDocumentDirectory or pack stuff into NSUserDefaults.
It looks like you're trying to read in an entire struct in one slurp. Generally, you can't assume that memory layouts will match up across architectures; endianness, data sizes, and padding are likely to be different. You need to read each struct field individually into a known-size entity (such as those defined in stdint.h), swap endianness if necessary, then populate your struct manually.
However, the above is a guess since all you've said about fread and fwrite is that they "don't work". What specifically isn't working about them?
However, the above is a guess since all you've said about fread and fwrite is that they "don't work". What specifically isn't working about them?
I am aware of the endian issue, so for the sake of simplicity let's just say that I'm trying to write a single int to a file using fwrite(). My desire is for fwrite() to create a file in the app's bundle containing the int data.
Frank says you can't write directly into an apps bundle so I guess this itself is the source of my problems.
In my game, all I'm trying to do is keep track of a few simple integer variables(in a single file) when the player prematurely exits a game. Given my low requirements, what would be the absolute simplest way to achieve this, I did look at both NSDocumentDirectory and NSUserDefaults but still wonder if there is not a simpler solution.
Frank says you can't write directly into an apps bundle so I guess this itself is the source of my problems.
In my game, all I'm trying to do is keep track of a few simple integer variables(in a single file) when the player prematurely exits a game. Given my low requirements, what would be the absolute simplest way to achieve this, I did look at both NSDocumentDirectory and NSUserDefaults but still wonder if there is not a simpler solution.
Something tells me you didn't read the documentation. Using NSUserDefaults would require two lines of code to write and read an integer.
And you can't write to the application bundle because that bundle has been encrypted, and if you change its bytes, it can't be decrypted to run.
And you can't write to the application bundle because that bundle has been encrypted, and if you change its bytes, it can't be decrypted to run.
As long as you're using Cocoa, definitely try using NSUserDefaults, you'll love it! 
... shouldn't ever write to the bundle on Mac either, BTW.
I just kinda made this up right now, so I don't know if it'd actually work on iPhone (don't know why it wouldn't though, but I'm tired and not in my right mind ATM
), but you could try something like this to change the current working directory so that your fwrite/fread stuff works out of the documents directory:

... shouldn't ever write to the bundle on Mac either, BTW.
I just kinda made this up right now, so I don't know if it'd actually work on iPhone (don't know why it wouldn't though, but I'm tired and not in my right mind ATM
), but you could try something like this to change the current working directory so that your fwrite/fread stuff works out of the documents directory:Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
const char *cwdPath = [documentsDirectory UTF8String];
chdir(cwdPath);
Then I guess NSUserDefaults it is. Thanks for all the help.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| iPhone High Score Data File/Sort | Art and Structure | 4 | 3,405 |
Jul 4, 2009 01:42 PM Last Post: Art and Structure |
|
| Howe can download any file from server to the iPhone application? | Rajneesh84 | 3 | 4,186 |
Feb 22, 2009 10:53 PM Last Post: AnotherJake |
|

