• 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: [Newbie] writing plain text to a file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Newbie] writing plain text to a file


  • Subject: Re: [Newbie] writing plain text to a file
  • From: "John C. Randolph" <email@hidden>
  • Date: Fri, 28 Feb 2003 18:23:10 -0800

On Friday, February 28, 2003, at 01:27 PM, Chris Garaffa wrote:

Hi all,

I've been programming Cocoa for a couple months now, but getting really into it the last 2 or 3 weeks. I've got this program which generates a random number in a specified interval, and writes that number, and the next 100 or so numbers above it. It needs to write all these numbers to a file, each on its own line. What I do now is create an NSMutableArray to hold all the numbers, and then [numArray writeToFile: numFileLocation atomically:YES]. This works... it creates the file and the data is in there, but I (and my users!) would rather have just a plain text file with one number on each line instead of this XML file.
I did some research and found a lot of stuff about NSFileHandle, NSFileManager and NSData but I couldn't figure out the best way to do this. Can someone point me in the right direction? Thanks!

NSString *path; // fill these in however you like.
NSMutableArray *numArray;
size_t someBigNumber = 500; // doesn't *really* matter how big this is, it will grow if needed.

NSMutableString *results = [NSMutableString stringWithCapacity:someBigNumber];

NSEnumerator *items = [myArray objectEnumerator];

id thisItem;

while (thisItem = [items nextObject])
{
[results appendString:[thisItem stringValue]];
[results appendString:@"\n"];
}

[results writeToFile:path atomically:YES];

Hope this helps,

-jcr


John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: [Newbie] writing plain text to a file
      • From: Scott Anguish <email@hidden>
References: 
 >[Newbie] writing plain text to a file (From: Chris Garaffa <email@hidden>)

  • Prev by Date: RE: [Newbie] writing plain text to a file
  • Next by Date: Re: [Newbie] writing plain text to a file
  • Previous by thread: RE: [Newbie] writing plain text to a file
  • Next by thread: Re: [Newbie] writing plain text to a file
  • Index(es):
    • Date
    • Thread