Re: <no subject>
Re: <no subject>
- Subject: Re: <no subject>
- From: Ondra Cada <email@hidden>
- Date: Thu, 31 Jan 2002 22:18:20 +0100
Ben,
>
>>>>> Ben Mackin (BM) wrote at Thu, 31 Jan 2002 11:25:15 -0800:
BM> I am wondering how I would go about opening a text file and reading in
BM> one word at a time. In C++, it is as simple as:
BM>
BM> fstream myFile;
BM> string myString;
BM>
BM> myFile.open("mytext.txt", ios::in);
BM>
BM> myFile >> myString;
BM>
BM> and myString contains that last word from the file, and the cursor is
BM> moved so that when I call myFile >> myString again, it retrievs the next
BM> word.
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++).
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.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc