Re: Simple File Data Reading Question
Re: Simple File Data Reading Question
- Subject: Re: Simple File Data Reading Question
- From: Sam Daniel <email@hidden>
- Date: Wed, 11 Feb 2004 22:51:51 -0700
Hi Prachi and all others who responded,
Thank you for your rather specific suggestion. Having been alerted
about NSScanner by others and perused recommended references, I could
see exactly how to make use of your code along with fragments from
others. As you can see below, I incorporated all necessary code into a
button action, where mins is the NSMutableArray instance variable of
interest. I have tested this and it works with any white space
delimiters and any number of lines of data containing any number of
integers each. One could certainly generalize this implementation to
read different types...
- (IBAction)readFileData:(id)sender
{
int anInt;
NSString *fileContents = [NSString stringWithContentsOfFile:fileName];
NSScanner *scanner = [NSScanner scannerWithString:fileContents];
[scanner setCharactersToBeSkipped:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];
mins = [NSMutableArray array];
while([scanner isAtEnd] == NO) {
[scanner scanInt:&anInt];
[mins addObject:[NSNumber numberWithInt:anInt]];
}
[mins retain];
NSLog(@"mins : %@", mins);
}
This is a lot simpler than I could have done on my own because I was
approaching the problem at too elementary a level. In the process I
have learned how to read and derive some benefit from related
references.
Thanks again to you and all.
Regards,
Sam Daniel
On Feb 11, 2004, at 12:11 PM, Prachi Gauriar wrote:
>
>
On Feb 11, 2004, at 12:42 PM, Sam Daniel wrote:
>
>
>> (BOOL)readFromFile:(NSString *)fileName ofType(NSString *)docType
>
>
>
> However, by using @" " as the component delimiter, I found it not as
>
> useful as one might want. I was surprised to find out that the code
>
> worked fine for multi-line entries of 3 integers separated by one
>
> space, assuming that the first line had no leading space at all!
>
> What is needed here is a means to filter white space or any other
>
> delimiters. After scouring the NSString and NSCharacterSet classes,
>
> I could not find what I needed to accomplish this easily. It appears
>
> that one would have to write a special method that looks at all the
>
> characters of the retrieved string and extract the identified
>
> components. When I did that I was surprised to see what appear to be
>
> unicode characters (although I don't know what those are) for which
>
> the content did not have a one to one correspondence. I will keep
>
> trying.
>
>
How about NSScanner?
>
>
It seems like it could handle this quite easily:
>
>
NSScanner *scanner = [NSScanner scannerWithString:contentsOfFile];
>
int firstInt, secondInt, thirdInt;
>
>
[scanner setCharactersToBeSkipped:[NSCharacterSet
>
whitespaceAndNewlineCharacterSet]];
>
>
[scanner scanInt:&firstInt];
>
[scanner scanInt:&secondInt];
>
[scanner scanInt:&thirdInt];
>
>
>
Hope that helps.
>
>
-Prachi
_______________________________________________
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.