iostream.stream.readn
Reads a value of the given numeric format and returns it
from "iostream" import blob
let hp = blob(0)
hp.writen(100, 'i')
hp.seek(0)
println("hp.readn('i') =", hp.readn('i'))hp.readn('i') = 100Parameters
format | int | a character selecting the numeric type and width to read |
Return value
An int for every integer format, a float for 'f' and 'd'.
'b'-unsigned char, 1 byte'c'-signed char, 1 byte'w'-unsigned short, 2 bytes's'-short, 2 bytes'i'- a 32-bit signed integer, 4 bytes'l'- the VM's native integer, 8 bytes (this build always uses a 64-bit integer, even when the target is 32-bit)'f'- a 32-bit IEEE float, 4 bytes'd'- a 64-bit IEEE double, 8 bytes, narrowed to the 32-bitfloatthis build'snumberuses
writen writes the same eight codes at the same widths.
Bytes are copied verbatim, in the host's native byte order (little-endian on every platform this engine targets); readn never swaps bytes on its own. A value written and read back on the same platform always round-trips, but exchanging the raw bytes with a big-endian peer needs a byte swap first, such as iostream.swap2 or iostream.swap4.
Errors
Throws io error when fewer bytes than the format's width remain before len.
Throws invalid format when format is not one of the eight codes above.
Notes
The same method works on a file: it reads at the file's current position and advances it by the format's width, the same as it moves a blob's cursor.
Example
from "iostream" import blob
let b = blob(0) // grows to fit as each write extends past the end
b.writen(0x0102030405060708, 'l') // 8-byte native integer
b.writen(-7, 'c') // 1-byte signed
println("b.len() =", b.len()) // 8 + 1 = 9
b.seek(0)
println("b.readn('l') == 0x0102030405060708 =", b.readn('l') == 0x0102030405060708)
println("b.readn('c') =", b.readn('c'))
b.seek(-1, 'e') // 1 byte left before len, 'i' needs 4
try { b.readn('i') } catch (e) { println("b.readn('i') throws:", e) }
try { b.readn('z') } catch (e) { println("b.readn('z') throws:", e) } // 'z' is not a format codeb.len() = 9
b.readn('l') == 0x0102030405060708 = true
b.readn('c') = -7
b.readn('i') throws: io error
b.readn('z') throws: invalid formatSee also
readn | Reads a value of the given numeric format and returns it |
writen | Writes a numeric value in the given format |
seek | Seeks to the given offset; origin is 'b' (begin), 'c' (current) or 'e' (end) |
swap2 | Byte-swaps a 16-bit value |
| stream | class index |