Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
I've been reading my library of books, searching online via google extensively, going over archives, going through my bookmarks, but I still don't know how to save a simple text files, then open it again with my application. For learning purposes I have a bare bones document based cocoa app, where the gui is simply a text field. I want to be able to save that text field to a .txt file. But the problem is, I can't find the right tutorial/example for me. Apple's documentation explains WHAT core data is but not how to use it. Online stuff I was able to follow, only to find out at compile time that it's too old of a version and doesn't work. And I'm not far enough in my book yet to do the chapter there. Can anyone help? I'm looking for the simplest example ever, that I can then go over minutely and examine and learn from and ask questions about. Thanks!
 

Cromulent

macrumors 604
Oct 2, 2006
6,813
1,100
The Land of Hope and Glory
I've been reading my library of books, searching online via google extensively, going over archives, going through my bookmarks, but I still don't know how to save a simple text files, then open it again with my application. For learning purposes I have a bare bones document based cocoa app, where the gui is simply a text field. I want to be able to save that text field to a .txt file. But the problem is, I can't find the right tutorial/example for me. Apple's documentation explains WHAT core data is but not how to use it. Online stuff I was able to follow, only to find out at compile time that it's too old of a version and doesn't work. And I'm not far enough in my book yet to do the chapter there. Can anyone help? I'm looking for the simplest example ever, that I can then go over minutely and examine and learn from and ask questions about. Thanks!

Core data is the wrong framework I would have thought for things like this.

http://developer.apple.com/documentation/Cocoa/Conceptual/AppFileMgmt/AppFileMgmt.html

http://developer.apple.com/documentation/Cocoa/Conceptual/LowLevelFileMgmt/LowLevelFileMgmt.html

Edit : having done a bit of reading on the subject I guess you can do some cool stuff with Core Data, the question is do you really need to? If all you are doing is simple text file stuff you could probably even get away with using the C functions. It just depends how complex you want your app and what ideas you have in mind.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
Cromulent is right, by looking at CoreData you're really overshooting the problem.

The simplest method is to use the C standard IO library. For example, the code segment:

#include <stdio.h>
..
..
FILE *fp;
..
fp = fopen ("/tmp/somefile", w+);
fprintf(fp, "%s", "I'm somefile");
...


Creates a file in /tmp called somefile and writes to it the text "I'm somefile".

A standard reference for all this is:
GNU C Library

or, in a book form which I highly recommend:
APUE
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
I've been trying to find just ANYTHING to get started in saving data. Core data is the only type of file saving I could find that was mentioned, though I knew that plain C must have some. Thanks guys I will look into those links :)
 

white89gt

macrumors regular
Jan 18, 2006
133
0
I've been working on this a lot lately, and at the moment I'm just using the writeToFile and initWithContentsOfFile methods of the NSArray and NSMutableArray classes (I think NSString has the same methods, but I'm not sure). There are better ways, but it's pretty damned easy this way.
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
white could you please supply a line of code? I get so messed up trying to understand the documentation...
 

white89gt

macrumors regular
Jan 18, 2006
133
0
Here you go. Here's code to save a string to a file. It puts it in the root of your harddrive. You can specify a path to the file if you want.

Code:
NSString *myString = [NSString stringWithFormat:@"Save this"];
[myString writeToFile:@"myFile.txt" atomically:YES];

And this loads it.
Code:
NSString *myString = [NSString initWithContentsOfFile:@"myFile.txt"];

There's more to it (like using NSFileManagers to check if a file exists, or searching for a particular user's documents directory), but that will get you started. There are also several different version of the writeToFile method, some of which are deprecated. The one that I showed is the most basic, and might throw a warning due to deprecation. It will work, though. I also worked on figuring out NSKeyedArchivers and NSKeyedUnarchivers last night, and they're almost just as easy. However, you won't really need those until you start getting ready to save arrays of custom NSObject subclasses.
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
thanks :) and now that I have access to my computer with the xcode documentation I can look around.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.