iPhone SDK Read/Writing to Files - MacRumors Forums
Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 14, 2008, 03:11 AM   #1
zcarter
macrumors member
 
Join Date: Apr 2007
iPhone SDK Read/Writing to Files

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
zcarter is offline   0 Reply With Quote
Old Mar 14, 2008, 06:11 AM   #2
Jeremy1026
macrumors 68000
 
Join Date: Nov 2007
I think you would have to use an SQlite Data Base, but I could very well be wrong.
__________________
1026 Development

Last edited by Mitthrawnuruodo; Feb 19, 2009 at 05:00 AM. Reason: No need to quote the entire first post...
Jeremy1026 is offline   0 Reply With Quote
Old Mar 14, 2008, 07:24 AM   #3
gifford
macrumors 6502
 
Join Date: Jun 2007
Location: Miserable England
see http://forums.macrumors.com/showthread.php?t=453043

Also i presume u could look at NSData
__________________
WANTED: ADBE tried for crimes against humanity.

Last edited by gifford; Mar 14, 2008 at 08:39 AM.
gifford is offline   0 Reply With Quote
Old Mar 14, 2008, 09:47 AM   #4
cmaier
macrumors 604
 
Join Date: Jul 2007
Location: California
Quote:
Originally Posted by gifford View Post
see http://forums.macrumors.com/showthread.php?t=453043

Also i presume u could look at NSData
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)).
cmaier is offline   0 Reply With Quote
Old Mar 14, 2008, 10:26 AM   #5
admanimal
macrumors 68040
 
Join Date: Apr 2005
If you are just interested in essentially dumping a string to a text file, you can use NSString writeToFile:
admanimal is offline   0 Reply With Quote
Old Mar 14, 2008, 10:43 AM   #6
fishkorp
macrumors 68000
 
Join Date: Apr 2006
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.
fishkorp is offline   0 Reply With Quote
Old Mar 14, 2008, 12:49 PM   #7
zcarter
Thread Starter
macrumors member
 
Join Date: Apr 2007
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?
zcarter is offline   0 Reply With Quote
Old Mar 14, 2008, 01:10 PM   #8
iSee
macrumors 68030
 
iSee's Avatar
 
Join Date: Oct 2004
Read this (you'll have to log in as a registered developer):

Link: https://developer.apple.com/iphone/l...section_3.html
iSee is offline   0 Reply With Quote
Old Mar 14, 2008, 04:41 PM   #9
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Side question: does iPhone support preferences/defaults? Are there UIDefaults and UINotificationCenter classes?
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Mar 14, 2008, 04:47 PM   #10
admanimal
macrumors 68040
 
Join Date: Apr 2005
Quote:
Originally Posted by zcarter View Post
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?
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.
admanimal is offline   0 Reply With Quote
Old Oct 31, 2008, 07:48 PM   #11
lg7
macrumors newbie
 
Join Date: Oct 2008
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.
lg7 is offline   0 Reply With Quote
Old Feb 18, 2009, 06:29 AM   #12
riruilo
macrumors newbie
 
Join Date: Sep 2008
Quote:
Originally Posted by admanimal View Post
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.
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.
riruilo is offline   0 Reply With Quote
Old Feb 18, 2009, 11:25 AM   #13
ghayenga
macrumors regular
 
Join Date: Jun 2008
Quote:
Originally Posted by HiRez View Post
Side question: does iPhone support preferences/defaults? Are there UIDefaults and UINotificationCenter classes?
Yes to both.
ghayenga is offline   0 Reply With Quote
Old Feb 18, 2009, 02:06 PM   #14
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Quote:
Originally Posted by riruilo View Post
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.

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.
lazydog is offline   0 Reply With Quote
Old Feb 18, 2009, 03:25 PM   #15
riruilo
macrumors newbie
 
Join Date: Sep 2008
Quote:
Originally Posted by lazydog View Post
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.
Yep, I already knew that
Anyway thanks.
Any idea?
riruilo is offline   0 Reply With Quote
Old Feb 18, 2009, 04:02 PM   #16
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
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
lazydog is offline   0 Reply With Quote
Old Dec 2, 2009, 10:00 AM   #17
sm4ck
macrumors newbie
 
Join Date: Dec 2009
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!
sm4ck is offline   0 Reply With Quote
Old Dec 2, 2009, 10:15 AM   #18
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Dec 2, 2009, 11:48 AM   #19
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
This works for me:

Code:
std::string str;
str = [[NSSearchPathForDirectoriesInDomains(  NSDocumentDirectory
                                            , NSUserDomainMask
                                            , YES) objectAtIndex:0] UTF8String];
lloyddean is offline   0 Reply With Quote
Old Dec 2, 2009, 04:26 PM   #20
drf1229
macrumors regular
 
Join Date: Jun 2009
Quote:
Originally Posted by admanimal View Post
If you are just interested in essentially dumping a string to a text file, you can use NSString writeToFile:
When I try and use NSString writeToFile: I get "Warning: writeToFile was depreciated in Xcode..."
drf1229 is offline   0 Reply With Quote
Old Dec 2, 2009, 06:55 PM   #21
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
Code:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;
Not deprecated.
PhoneyDeveloper is offline   0 Reply With Quote
Old Dec 2, 2009, 06:56 PM   #22
drf1229
macrumors regular
 
Join Date: Jun 2009
Oh writeToFile:atomically was depreciated not writeToFile:atomically:encoding:error:
drf1229 is offline   0 Reply With Quote
Old Dec 2, 2009, 07:42 PM   #23
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Dec 2, 2009, 07:59 PM   #24
drf1229
macrumors regular
 
Join Date: Jun 2009
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?
drf1229 is offline   0 Reply With Quote
Old Dec 2, 2009, 09:29 PM   #25
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
You have to give more details. And what does the NSError* say?
PhoneyDeveloper is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
writing midi files Chirone iPhone/iPad Programming 0 Jun 5, 2013 06:51 PM
Apple TV - Read HD Files rodrigoodin Jailbreaks and iOS Hacks 2 Feb 11, 2013 07:41 PM
Can you read audio files for flash drive on your iPhone? snarestud940 iPhone 1 Jan 24, 2013 10:55 PM
Writing files to Documents Dir xArtx iPhone/iPad Programming 13 Nov 22, 2012 10:15 PM
PHP Read Files jacob.3336 Web Design and Development 1 Oct 29, 2012 09:38 PM

Forum Jump

All times are GMT -5. The time now is 03:03 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC