| 1 |
//--------------------------------------------------------------------- |
| 2 |
// read text tokens |
| 3 |
// adapted from examples/io/read-tokens.ck |
| 4 |
//--------------------------------------------------------------------- |
| 5 |
|
| 6 |
// path and name of file to read |
| 7 |
me.dir() + "jabberwocky.txt" => string filename; |
| 8 |
// or get filename from command line arguments, if any |
| 9 |
if( me.args() > 0 ) me.arg(0) => filename; |
| 10 |
|
| 11 |
// instantiate a file IO object |
| 12 |
FileIO fio; |
| 13 |
// a string tokenizer |
| 14 |
StringTokenizer tokenizer; |
| 15 |
// a line of text |
| 16 |
string line; |
| 17 |
// a word token |
| 18 |
string word; |
| 19 |
|
| 20 |
// open the file |
| 21 |
fio.open( filename, FileIO.READ ); |
| 22 |
|
| 23 |
// ensure it's ok |
| 24 |
if( !fio.good() ) |
| 25 |
{ |
| 26 |
cherr <= "can't open file: " <= filename <= " for reading..." |
| 27 |
<= IO.newline(); |
| 28 |
me.exit(); |
| 29 |
} |
| 30 |
|
| 31 |
// new line |
| 32 |
cherr <= IO.newline(); |
| 33 |
|
| 34 |
// loop until end |
| 35 |
while( fio.more() ) |
| 36 |
{ |
| 37 |
// read current line |
| 38 |
fio.readLine() => line; |
| 39 |
// for each line, tokenize |
| 40 |
tokenizer.set( line ); |
| 41 |
// loop over tokens |
| 42 |
while( tokenizer.more() ) |
| 43 |
{ |
| 44 |
// get the next word |
| 45 |
tokenizer.next() => word; |
| 46 |
// print word followed by a space |
| 47 |
cherr <= word <= " "; |
| 48 |
} |
| 49 |
// done with current line |
| 50 |
cherr <= IO.newline(); |
| 51 |
} |