Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikesown

macrumors member
Original poster
Aug 12, 2004
37
0
Right now, I'm working on a translator of sorts. Basically it takes in words, and reads them against a database(a text file with commas seperating what is entered compared to the translation). Basically, I need to iterate line by line through a file. Unfortunately, I don't have a clue as to how to read a file in Objective C. I'm familiar with the FileReader class in Java. Is there anything like this in Objective C?

Thanks!
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
A simplistic approach might use something like this:

Code:
NSString *fileString = [NSString stringWithContentsOfFile:aFilePath]; // reads file into memory as an NSString
NSArray *lines = [fileString componentsSeparatedByString:@"\n"]; // each line, adjust character for line endings
That would probably work if you happen to know what line endings your source file uses. If the line ending type is unknown, it's more complicated (UNIX vs. Mac line endings, Unicode, UTF-8, etc.). You can also use that same technique to separate each line into words (again, if the separator is known). Also have a look at the NSScanner class for dealing with this, which may offer some more flexibility.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.