| 1 |
//--------------------------------------------------------------------- |
| 2 |
// name: read-byte.ck |
| 3 |
// desc: example of reading raw bytes from file |
| 4 |
// NOTE: file must be opened in BINARY mode for this to work |
| 5 |
//--------------------------------------------------------------------- |
| 6 |
|
| 7 |
// default filepath |
| 8 |
me.dir() + "read-byte.txt" => string filename; |
| 9 |
// look at command line |
| 10 |
if( me.args() > 0 ) me.arg(0) => filename; |
| 11 |
|
| 12 |
// instantiate |
| 13 |
FileIO fio; |
| 14 |
// open a file (in BINARY mode) |
| 15 |
fio.open( filename, FileIO.READ | FileIO.BINARY ); |
| 16 |
// ensure it's ok |
| 17 |
if( !fio.good() ) |
| 18 |
{ |
| 19 |
cherr <= "can't open file: " <= filename <= " for reading..." |
| 20 |
<= IO.newline(); |
| 21 |
me.exit(); |
| 22 |
} |
| 23 |
|
| 24 |
// variable to read into |
| 25 |
int val; |
| 26 |
|
| 27 |
// loop until end |
| 28 |
while( fio.more() ) |
| 29 |
{ |
| 30 |
// read the next byte |
| 31 |
fio.readInt( IO.INT8 ) => val; |
| 32 |
// optionsss ^^^^^^^ | requires chuck-1.5.0.1 or higher |
| 33 |
// IO.INT8, IO.INT16, IO.INT32 (default to unsigned) |
| 34 |
// IO.UINT8, IO.UINT16, IO.UINT32 (explicitly unsigned) |
| 35 |
// IO.SINT6, IO.SINT16, IO.SINT32 (explicitly signed) |
| 36 |
|
| 37 |
// print the value |
| 38 |
cherr <= val <= IO.newline(); |
| 39 |
} |