PDA

View Full Version : iPhone SDK Read/Writing to Files




zcarter
Mar 14, 2008, 03:11 AM
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



Jeremy1026
Mar 14, 2008, 06:11 AM
I think you would have to use an SQlite Data Base, but I could very well be wrong.

gifford
Mar 14, 2008, 07:24 AM
see http://forums.macrumors.com/showthread.php?t=453043

Also i presume u could look at NSData

cmaier
Mar 14, 2008, 09:47 AM
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)).

admanimal
Mar 14, 2008, 10:26 AM
If you are just interested in essentially dumping a string to a text file, you can use NSString writeToFile:

fishkorp
Mar 14, 2008, 10:43 AM
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.

zcarter
Mar 14, 2008, 12:49 PM
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?

iSee
Mar 14, 2008, 01:10 PM
Read this (you'll have to log in as a registered developer):

Link: https://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ApplicationEnvironment/chapter_6_section_3.html

HiRez
Mar 14, 2008, 04:41 PM
Side question: does iPhone support preferences/defaults? Are there UIDefaults and UINotificationCenter classes?

admanimal
Mar 14, 2008, 04:47 PM
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.

lg7
Oct 31, 2008, 07:48 PM
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.

riruilo
Feb 18, 2009, 06:29 AM
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.

ghayenga
Feb 18, 2009, 11:25 AM
Side question: does iPhone support preferences/defaults? Are there UIDefaults and UINotificationCenter classes?

Yes to both.

lazydog
Feb 18, 2009, 02:06 PM
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.



//
// 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.

riruilo
Feb 18, 2009, 03:25 PM
This is what I'm using to read files from the resources... not sure if you'd want to save data there mind.



//
// 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?

lazydog
Feb 18, 2009, 04:02 PM
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

sm4ck
Dec 2, 2009, 10:00 AM
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!

PhoneyDeveloper
Dec 2, 2009, 10:15 AM
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.

lloyddean
Dec 2, 2009, 11:48 AM
This works for me:


std::string str;
str = [[NSSearchPathForDirectoriesInDomains( NSDocumentDirectory
, NSUserDomainMask
, YES) objectAtIndex:0] UTF8String];

drf1229
Dec 2, 2009, 04:26 PM
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..."

PhoneyDeveloper
Dec 2, 2009, 06:55 PM
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;

Not deprecated.

drf1229
Dec 2, 2009, 06:56 PM
Oh writeToFile:atomically was depreciated not writeToFile:atomically:encoding:error:

PhoneyDeveloper
Dec 2, 2009, 07:42 PM
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.

drf1229
Dec 2, 2009, 07:59 PM
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?

PhoneyDeveloper
Dec 2, 2009, 09:29 PM
You have to give more details. And what does the NSError* say?

sm4ck
Dec 3, 2009, 05:22 AM
Thanks for the reply, but are you saying that NSSearchPathForDirectoriesInDomains works in a C++ file? It works just fine in my objective c but I cannot get this to work in a C++ file.

This works for me:


std::string str;
str = [[NSSearchPathForDirectoriesInDomains( NSDocumentDirectory
, NSUserDomainMask
, YES) objectAtIndex:0] UTF8String];

sm4ck
Dec 3, 2009, 05:29 AM
Thanks Phoney but could you be a little more specific. I tried something like this, basically created a Global.h file that got the path using the NSSearch function but as soon as I included it in my C++ file it threw all sorts of errors. Any additional input would be great.

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
Dec 3, 2009, 11:31 AM
I don't really have code handy for this but it would go something like this:

// Utilities.h
const char * DocumentsFolder(void); // returns full path to Documents folder

// Utilities.m
const char * DocumentsFolder(void)
{
NSArray* documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* path = nil;

if (documentDir)
{
path = [documentDir objectAtIndex:0];
}

return [path fileSystemRepresentation];
}

Utilities.h has no Objective-C code in it and can be included in your .cpp files.

There are other ways to manipulate this kind of code but this is the core of it.

sm4ck
Dec 3, 2009, 11:59 AM
Awesome! Thanks, that makes a lot of sense. I'll give that a try and post my code when I get it working.

lloyddean
Dec 3, 2009, 02:18 PM
Thanks for the reply, but are you saying that NSSearchPathForDirectoriesInDomains works in a C++ file? It works just fine in my objective c but I cannot get this to work in a C++ file.

No, an ObjC++ file, which has the extension .mm.

I do cross-platform stuff in C/C++ and join it with Cocoa (on Mac OS) where it needs to meet the GUI. So I fairly regularly mix C (.c), C++ (.cpp), ObjC++ (.mm) and ObjC (.m) code in projects.

drf1229
Dec 3, 2009, 02:41 PM
You have to give more details. And what does the NSError* say?

I actually fixed my problem. See the problem was I was trying to read from the file even though it didn't exist, not an issue with writeToFile. Thanks anyway!

knowledgebanc
Dec 8, 2009, 07:55 PM
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!

http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html