iPhone SDK Read/Writing to Files

Discussion in 'iOS Programming' started by zcarter, Mar 14, 2008.

  1. macrumors member

    Joined:
    Apr 21, 2007
    #1
    Hey guys..

    I'm trying to make a to-do list and I need to know how to save data in anyway shape or form. My first thought was to figure out how to Read/Write to files, but I checked for classes on the iPhone reference forms and only found NSFileHandle, NSObject, NSFile, etc..None of them seemed to do the trick. Could anybody provide me with an example of how to save Data to a file..or save data at all if there's a better way..


    Thanks a lot,

    Zac
     
  2. macrumors 68000

    Joined:
    Nov 3, 2007
    #2
    I think you would have to use an SQlite Data Base, but I could very well be wrong.
     
  3. macrumors 6502

    gifford

    Joined:
    Jun 11, 2007
    Location:
    Miserable England
  4. macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #4
    An easier way than sql, particularly given the small amount of data in a to-do list, would be to serialize or archive your data.

    For example, you can automatically write and initFrom NSDictionary's, NSArray's, etc. If you have a nested data structure you can provide callbacks to serialize or archive each element.

    I don't remember the calls, but search the documentation for "archive" or "serialize."

    (Note: I'm also trying to decide on how to save data. I'm wondering if it is better to save each record in its own file (time machine style) or not. Still trying to figure out the tradeoffs - I am also assuming device-wide search will come along, and am wondering what will make my future life easiest)).
     
  5. macrumors 68040

    Joined:
    Apr 22, 2005
    #5
    If you are just interested in essentially dumping a string to a text file, you can use NSString writeToFile:
     
  6. macrumors demi-god

    fishkorp

    Joined:
    Apr 10, 2006
    Location:
    Ellicott City, MD
    #6
    The Apple developer how-tos tell you which method to use to get the directory you can write to.

    For the app I'm working on I'm using SQLite and set that directory as the directory to store the database. All seems to work well.
     
  7. thread starter macrumors member

    Joined:
    Apr 21, 2007
    #7
    Directories..

    If for example I put a file in the folder Resources called data.txt...What would the directory be?

    /Resources/data.txt doesn't work
    data.txt doesn't work
    Resources/data.txt doesn't work
    /data.txt doesn't work

    Any ideas why?
     
  8. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
  9. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #9
    Side question: does iPhone support preferences/defaults? Are there UIDefaults and UINotificationCenter classes?
     
  10. macrumors 68040

    Joined:
    Apr 22, 2005
    #10
    If you are talking about putting the file in the Resources directory in XCode, doing so puts the file into the application bundle itself once you compile it. Generally speaking this is -not- where you want to save user data, particularly on an iPhone. The iPhone has very specific places that it allows you to save data.
     
  11. lg7
    macrumors newbie

    Joined:
    Oct 31, 2008
    #11
    Writing data explanation

    I'm new to both the Mac/iPhone platform and the Objective C 2.0 programming language.

    Could someone please explain the code posted above to write data using the iPhone SDK. I'd like to write data entered by the user in text boxes to a text file and I am lost.

    I'd like to have a table view with a number of entries that a user can select and see/edit details for in a new window with several text boxes.

    Hopefully this is clear enough; I can clarify about any of the above if necessary. Thanks in advance.
     
  12. macrumors newbie

    Joined:
    Sep 28, 2008
    #12
    Hi!

    Could you tell me in which folders should I store my ranking?
    I'm doing a game in C++, so I don't use COCOA classes.
    I would like to use read/write or std, I now it works, but I don't know where to create or read my file.BTW, I only know resource folders.

    Thanks a lot for suggestions.
     
  13. macrumors regular

    Joined:
    Jun 18, 2008
    #13
    Yes to both.
     
  14. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #14

    This is what I'm using to read files from the resources... not sure if you'd want to save data there mind.

    Code:
    
    //
    // Get the path to the resources.
    //
    NSString* resources = [ [ NSBundle mainBundle ] resourcePath ] ;	
    
    std::string respath( [ resources UTF8String ] ) ;
    
    std::string fpath = respath + "/" + "a-file.txt" ;
    
    
    b e n

    EDIT: Actually after reading your question again I think I haven't posted anything useful.
     
  15. macrumors newbie

    Joined:
    Sep 28, 2008
    #15
    Yep, I already knew that :)
    Anyway thanks.
    Any idea?
     
  16. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #16
    I think you need the document directory. Have a look at the iPhone Application Programming Guide/File and Data Management. There's a code snippet that shows you how to get the path.

    b e n
     
  17. macrumors newbie

    Joined:
    Dec 2, 2009
    #17
    Saving Files with C++

    I realize this is an old post but really hoping someone on this thread can help me out.

    I'm trying to save a file to the iPhone documents directory using C++. I know the standard C++ file functions but don't know how to get the directory. The NSString, NSSearch, etc. referenced in all the examples are not available as it is not objective c. Any help would be appreciated.

    Thanks!
     
  18. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #18
    Write your own utility functions with C linkage that provide the full paths to the app bundle and the documents folder. You'll then be able to access these from your C++ code. You implement those functions using Objective-C.
     
  19. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #19
    This works for me:

    Code:
    std::string str;
    str = [[NSSearchPathForDirectoriesInDomains(  NSDocumentDirectory
                                                , NSUserDomainMask
                                                , YES) objectAtIndex:0] UTF8String];
    
     
  20. macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #20
    When I try and use NSString writeToFile: I get "Warning: writeToFile was depreciated in Xcode..."
     
  21. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #21
    Code:
    - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;
    Not deprecated.
     
  22. macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #22
    Oh writeToFile:atomically was depreciated not writeToFile:atomically:encoding:error:
     
  23. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #23
    Right. There was a period of time when apple decided to add NSError** parameters to lots of apis and also updated many of the NSString apis to use encodings. When they did those things they deprecated the older apis. This happened on MacOS X before iPhone OS was available but the headers are shared.

    In almost every case where you find a deprecated api there is a new api that replaces it or a new way of accomplishing the same task.
     
  24. macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #24
    Ah... I've been having some issues with "writeToFile" using NSMutableArray lately. For some odd reason I'm able to write to certain file names but not others. Any suggestions?
     
  25. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #25
    You have to give more details. And what does the NSError* say?
     

Share This Page