• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: append to file (new to Cocoa)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: append to file (new to Cocoa)


  • Subject: Re: append to file (new to Cocoa)
  • From: Ron Fleckner <email@hidden>
  • Date: Sat, 9 Jul 2005 01:12:16 +1000

Hi list,

After searching the docs and the list archive, I haven't been able to find out how to append data (text in my case) to a file. I'm making a fairly simple game and want to allow users to keep a record of their scores. This is my workaround:

// write new data to 'scores' file
NSString *theNewScore = [NSString stringWithFormat:@"%d\n", aInt];
NSError *err;
NSString *fileContents = [NSString stringWithContentsOfFile:pathToFile];
NSString *newFileContents = [fileContents stringByAppendingString:theNewScore];
[newFileContents writeToFile:pathToFile atomically:YES encoding:NSASCIIStringEncoding error:&err];


but is there a way I can simply append the text without first reading the whole file into a NSString variable and then using stringByAppendingString?

TIA for any pointers.

Ron


You might also want to have a look at NSUserDefaults. If its only a little data, like a list of highest scores, it would certainly do just fine - and you wouldn't have to deal with any file management at all.

j o a r

Would NSFileHandle be useful here? m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>


From: Jerry Krinock <email@hidden>

Cocoa has these handy -writeToFile methods in some of its classes such as
NSString, but they are not the normal way to write to a file. For
full-featured file I/O, look at the class NSFileHandle. Specifically, I
believe that -seekToEndOfFile and -writeData will do what you want.


You might also want make your user's score a "document", an NSDocument. You
get a lot of functionality built in.



Thanks j o a r, Jerry, and Matt, I think now I've got enough info to decide which approach to use. I don't really want to bother the user with saving files just to keep a tally of scores. Essentially I just want to be able to display the scores in something like a table or perhaps in a custom view. NSUserDefaults might be a good way to go IF it could support more than one game user using the same account on the one machine. It's very late here now and I'll look at both NSUserDefaults and NSFileHandle tomorrow.


Thanks again,
Ron
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: append to file (new to Cocoa)
      • From: j o a r <email@hidden>
  • Prev by Date: Re: Link error: zerolink
  • Next by Date: Re: New Documentation Available - Time to Fire Up Xcode
  • Previous by thread: Re: append to file (new to Cocoa)
  • Next by thread: Re: append to file (new to Cocoa)
  • Index(es):
    • Date
    • Thread