Re: reading in text files
Re: reading in text files
- Subject: Re: reading in text files
- From: Diggory Laycock <email@hidden>
- Date: Fri, 1 Feb 2002 00:15:47 +0000
NSString has the following method:
componentsSeparatedByString:
- (NSArray *)componentsSeparatedByString:(NSString *)separator
Returns an NSArray containing substrings from the receiver that have
been divided by separator. The substrings in the array appear in the
order they did in the receiver. If the string begins or ends with the
separator, the first or last substring, respectively, is empty. For
example, this code excerpt:
NSString *list = @"wrenches, hammers, saws";
NSArray *listItems = [list componentsSeparatedByString:@", "];
produces an array with these contents:
<File attached: image.tiff>
On Thursday, January 31, 2002, at 11:36 PM, Ben Mackin wrote:
>
on 1/31/02 1:18 PM, Ondra Cada at email@hidden wrote:
>
>
> I dunno C++, but this looks strange: where you, for example, defined
>
> word
>
> delimiters? But that's unimportant, of course (but that you sould be
>
> able to
>
> do exactly the same exploiting Objective-C++).
>
>
I don't define word delimiters. I just set myFile (an fstream) as the
>
input
>
stream and then read from it. C++ reads one word at a time (from white
>
space
>
to white space). Meaning if myFile was the following:
>
>
Hi a324 is a file to be read
>
>
Then the first call to myFile >> string would leave string with 'Hi',
>
and
>
then the next time I call myFile >> string, string would contain
>
'a324', and
>
etc.
>
>
I just want to do something like this in Obj-C
>
>
> BM> How do I do something like this in Objective-C?
>
>
>
> If the delimiter's as simple as a predefined string (like " ") and if
>
> the
>
> file is not tooo big, the simplest way is
>
>
>
> NSArray *a=[[NSString stringWithContentsOfFile:theFile]
>
> componentsSeparatedByString:@" "];
>
>
>
> If you need more impressive services, check NSScanner.
>
>
myFile is large (can be up to 16000 bytes), so I don't think reading the
>
whole thing into an NSString would be good.
>
>
I thought cocoa/objective-C was to be simple? If somehting is this easy
>
in
>
C++, it should be a snap in objective-C, right?
>
>
Thanks,
>
Ben
>
_______________________________________________
>
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.
>
>
Diggory Laycock - MonkeyFood.com
Tel: +44 7050 124080
(07050 124080 from UK)