file i/o urrrrrrrrrrgh!!!

Discussion in 'Mac Programming' started by liptonlover, Jul 23, 2008.

  1. liptonlover macrumors 6502a

    Mar 13, 2008
    I've had it. I want to be able to save variables to a file then retrieve that data. BUT I CAN'T!!! I have about 5 different links and I've been looking on google too but for a few weeks now I've been trying unsuccessfully. Can someone please give me a small WORKING demonstration that I can learn from? I don't know what else to do.
    Thanks in advance,
  2. cMacSW macrumors regular

    Mar 20, 2006
    Can you provide some info about what your trying to do? What Language? What type of data your trying to save. Post the code you've tried so far?
  3. liptonlover thread starter macrumors 6502a

    Mar 13, 2008

    I tried NSData. I tried NSFileWrapper. I tried NSFileHandle. I think there was more, like something about pipes. I've also got a couple links that I have or am starting to work through but so far are VERY disappointing.

    Sorry, I didn't keep any code...
  4. cMacSW macrumors regular

    Mar 20, 2006
    Did you try these instance methods

    To Write

    - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag 
    To Read

    - (id)initWithContentsOfFile:(NSString *)path
  5. liptonlover thread starter macrumors 6502a

    Mar 13, 2008
    actually now that you mention it I remember successfully using writeToFile: to save some text to a .txt file. But initWithContentsOfFile: didn't work... wouldn't load anything.

    I have a question though. When I saved the text to the file, it saved exactly what the NSString held. What I'm used to is that it pretty much saves the actual variable, so that I could use that variable as a pointer to the data in the file. I don't want to just print to a file, I want to save the actual variables so I can retrieve the correct data and put it into the correct variable. Do you understand what I'm saying? :confused:
  6. cMacSW macrumors regular

    Mar 20, 2006
    I'm not clear on the type of data your trying to read and write. If you could provide an example that would be helpful.
  7. yeroen macrumors 6502a


    Mar 8, 2007
    Cambridge, MA
    Are you saying you want to marshall or serialize an Object to non-volatile disk storage and reinstantiate it later, perhaps from another process?

    If so, then you need to look at Serializing Objects and

    Since you're just using an NSString, this may be overkill. All you may want to do is to convert the NSString to a C-style character array and write that to disk as plaintext. Then, you could read it back in to a character array, and construct another NSString from that array.
  8. liptonlover thread starter macrumors 6502a

    Mar 13, 2008
    ok... an example.

    I have a simple game of chess. I want to allow the user to save the location of all the pieces and who's turn it is. That means I need to either save the variables, or the objects that will in turn save the variables. In BASIC, I would:

    global x:int=5

    subroutine _save
    open "save.txt" for output as #1
    print #1,x
    close #1

    subroutine _load
    open "save.txt" for input as #1
    input #1,x
    close #1

    This would save the value of X, and when I load the data it would put the correct data in x even if I have more variables stored there being accessed.
  9. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
  10. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    It would be worth learning about serialization, but if you didn't want to it is pretty easy to encode this with a bunch of short ints or something similar. I would say you need 65 pieces of data if you were to store the state of each square and which player's turn is going on. This seems better to me because otherwise if you use 33 pieces of data to store information about which color's turn it is and where each piece is, you also have to track if the pawns have been promoted and to what. In the prior case, you just need to choose a number for each type of piece and its color. For example, 1-6 could be the black pieces and 7-12 could be the white pieces, with 0 indicating that space is vacant. If you need to track if castling is still legal for each side in a match, you need to track if the king and the rooks have moved. This is just 6 extra pieces of data that would just be 0 or 1.

    You'd end up with an encoding like:
    0/1: Is it what or black's turn?

    64 entries for pieces:
    0: vacant
    1: black pawn
    2: black rook
    3: black knight
    4: black bishop
    5: black queen
    6: black king
    7: white pawn
    8: white rook
    9: white knight
    10: white bishop
    11: white queen
    12: white king

    6 entries for castling reqs:
    0/1: white king has moved
    0/1: white rook at file a has moved
    0/1: white rook at file h has moved
    0/1: black king has moved
    0/1: black rook at file a has moved
    0/1: black rook at file h has moved

    You could probably contract this to 4, stating if castling is allowed with rooks at rank a or h for each color, but it may be easier to just keep all six.

    You could either assign a line for each of these valeus, or one line each except for the positions which would get one line, etc.

    As long as you picked a consistent encoding how you write it doesn't matter.

    Once you'd picked this actually reading/writing a file shouldn't be too hard. I'm not familiar with the cocoa methods for doing so, but I'd expect that you'd keep the game state someplace in the Library or something outside of your bundle.

    For C you'd just use fopen/fprintf/fread, etc. but others here might be able to help cocoa-fy this.

  11. liptonlover thread starter macrumors 6502a

    Mar 13, 2008
    I would much rather cocoa-fy it. Here's the structure I'm going for. (In reality my first game attempt will not by a chess game, but it will be similar.)


    int type
    int moves
    any more info I might need

    NSMutableArray *units (containing X units.)
    NSString *name
    any more info I might need

    I structured it like this so I could try some strategic thinking. As far as I know, I should be able to just save, serialize, archive, whatever you call it, the two teams and any extra data I might have, and they will save all their information going down to the units and their information. Correct?

    For now I just want the most basic saving routine. Preferably Cocoa. I will try writeToFile: and initWithContentsOfFile: but if it doesn't work...

    I will also see what I can do with stdio but last time I tried I believe that failed miserably.
  12. cMacSW macrumors regular

    Mar 20, 2006
    If you want to use cocoa then you will need to have your classes conform to the NSCoding protocol, and add initWithCoder and encodeWithCoder methods

    Then use keyed archiving to save and load your classes. If you maintain your state in your classes this would be the way to go.
  13. liptonlover thread starter macrumors 6502a

    Mar 13, 2008
    Can you please help me past that? I don't know where to begin. NSCoding info isn't being very helpful...
  14. cMacSW macrumors regular

    Mar 20, 2006
  15. liptonlover thread starter macrumors 6502a

    Mar 13, 2008
    I have cocoa programming for mac os x, the chapter it has on archiving needs... unarchiving. I don't really understand it. Thanks for that other link though, I'll read it now.

Share This Page