Prev | Current Page 590 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

For instance, to return each character from the file one by one, you would use readChar .
This method returns the characters as an integer value, which you can convert to a string character using
Std.chr . In this example, the returned character values are simply appended to the end of a haXe string:
class ReadChar
{
public static function main()
{
var stream = neko.io.File.read( ???example.txt???, false );
var str = ??????;
while ( !stream.eof() )
{
try
{
str += Std.chr( stream.readChar() );
}
catch ( err : Dynamic)
{
//neko.Lib.print( err );
}
}
neko.Lib.print( str );
stream.close();
}
}
As you can see, each readChar call exists within a try ??¦ catch block. This is because of the fact that
when the stream reaches the end of file ??” or eof ??” it throws an exception. The problem is you don ??™ t
306
Part II: Server Side, JavaScript, and Flash: Oh My!
know it ??™ s going to reach the end of file until it arrives, so you can only deal with it by trapping the error.
The test for the end of file in the while clause prevents an infinite loop caused by the try.


Pages:
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602