iphone file issue

Member
Posts: 194
Joined: 2009.02
Post: #1
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);
}
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #2
UTF8String returns const char*.
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #3
why would that be an issue?
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #4
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:
Code:
const char* file_path(NSString* path)
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *p;
    
    p = [bundle pathForResource:path fType:nil];

    return [p UTF8String];
}
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #5
Right, so then that wouldn't explain fread() and fwrite() not working.
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #6
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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #7
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?
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #8
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.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #9
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.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #10
As long as you're using Cocoa, definitely try using NSUserDefaults, you'll love it! Love

... 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 Rasp ), 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);
Quote this message in a reply
Member
Posts: 194
Joined: 2009.02
Post: #11
Then I guess NSUserDefaults it is. Thanks for all the help.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPhone High Score Data File/Sort Art and Structure 4 3,880 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,657 Feb 22, 2009 10:53 PM
Last Post: AnotherJake