Re: Cocoa:Reading legacy data files
Re: Cocoa:Reading legacy data files
- Subject: Re: Cocoa:Reading legacy data files
- From: Timothy Larkin <email@hidden>
- Date: Sun, 03 Mar 2002 07:52:05 -0500
On 2002.03.03 01:42, "Steven Rogers" <email@hidden> wrote:
>
I'm porting an application from Windows =:O and I need to read in a
>
structured ASCII file that the program parses with C code.
To solve a similar problem, I defined a document class, which has a method
for - (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)docType.
This is called as a result of the selection of File>Open, and provides the
path to the file in "fileName". The file consists of lines separated by
'\r' and fields separated by '\t'. So
- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)docType
{
NSArray *lines, *record;
NSString *tab = [NSString stringWithFormat:@"\t"];
NSString *s, *myText;
// Read the entire file into the string 'myText'.
myText = [NSString stringWithContentsOfFile:fileName];
// Parse the data into an array of strings separated by \r.
lines = [myText componentsSeparatedByString:[NSString
stringWithFormat:@"\r"]];
// enumerate the array, line by line
e = [lines objectEnumerator];
// 's' is the current lise
while (s = [e nextObject]) {
// 'record' is an array parsed from 's'.
record = [s componentsSeparatedByString:tab];
// Do something with record here....
}
return YES;
}
_______________________________________________
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.