| | 1 | #include <tads.h> |
| | 2 | |
| | 3 | main(args) |
| | 4 | { |
| | 5 | local fp; |
| | 6 | local buf; |
| | 7 | local siz; |
| | 8 | |
| | 9 | /* open a file */ |
| | 10 | fp = fileOpen('test.bin', 'rr'); |
| | 11 | if (fp == nil) |
| | 12 | { |
| | 13 | "Unable to open file\n"; |
| | 14 | return; |
| | 15 | } |
| | 16 | |
| | 17 | /* seek to the end to determine its size */ |
| | 18 | fileSeekEnd(fp); |
| | 19 | siz = fileGetPos(fp); |
| | 20 | fileSeek(fp, 0); |
| | 21 | |
| | 22 | /* create a byte array and read the entire file into the byte array */ |
| | 23 | buf = new ByteArray(siz); |
| | 24 | siz = fileRead(fp, buf); |
| | 25 | if (siz != buf.length()) |
| | 26 | "Short read: wanted <<buf.length()>> bytes, but only read <<siz>>\b"; |
| | 27 | |
| | 28 | /* show the entire thing */ |
| | 29 | for (local addr = 1 ; addr <= buf.length() ; addr += 16) |
| | 30 | { |
| | 31 | local c; |
| | 32 | |
| | 33 | /* show the current address in octal, with a zero base */ |
| | 34 | c = toString(addr - 1, 8); |
| | 35 | if (c.length() < 7) |
| | 36 | c = makeString('0', 7 - c.length()) + c; |
| | 37 | "<<c>> "; |
| | 38 | |
| | 39 | /* show this line */ |
| | 40 | for (local i = addr ; i < addr + 16 && i <= buf.length() ; ++i) |
| | 41 | { |
| | 42 | /* get a two-character hex representation of the byte */ |
| | 43 | c = toString(buf[i], 16); |
| | 44 | if (c.length() == 1) |
| | 45 | c = '0' + c; |
| | 46 | |
| | 47 | /* show it */ |
| | 48 | "<<c>> "; |
| | 49 | } |
| | 50 | |
| | 51 | "\n"; |
| | 52 | } |
| | 53 | |
| | 54 | /* done with the file */ |
| | 55 | fileClose(fp); |
| | 56 | |
| | 57 | /* open a file for writing */ |
| | 58 | fp = fileOpen('test2.bin', 'wr'); |
| | 59 | if (fp == nil) |
| | 60 | { |
| | 61 | "Unable to create output file\n"; |
| | 62 | return; |
| | 63 | } |
| | 64 | |
| | 65 | /* create a byte array */ |
| | 66 | buf = new ByteArray(16*256); |
| | 67 | |
| | 68 | /* fill it up */ |
| | 69 | for (local i = 0 ; i < 256 ; ++i) |
| | 70 | { |
| | 71 | /* fill a line of 16 bytes with this byte value */ |
| | 72 | for (local j = i*16+1 ; j <= (i+1)*16 ; ++j) |
| | 73 | buf[j] = i; |
| | 74 | } |
| | 75 | |
| | 76 | /* write it out */ |
| | 77 | fileWrite(fp, buf); |
| | 78 | |
| | 79 | /* done with the file */ |
| | 80 | fileClose(fp); |
| | 81 | } |