ARTICLE: "cookbook-io" "Input and output cookbook"
"Ask the user for their age, and print it back:"
{ $code
+ "USING: io math.parser ;"
": ask-age ( -- ) \"How old are you?\" print ;"
": read-age ( -- n ) readln string>number ;"
": print-age ( n -- )"
}
"Print the lines of a file in sorted order:"
{ $code
- "utf8 \"lines.txt\" file-lines natural-sort [ print ] each"
+ "USING: io io.encodings.utf8 io.files sequences sorting ;"
+ "\"lines.txt\" utf8 file-lines natural-sort [ print ] each"
}
"Read 1024 bytes from a file:"
{ $code
+ "USING: io io.encodings.binary io.files ;"
"\"data.bin\" binary [ 1024 read ] with-file-reader"
}
"Convert a file of 4-byte cells from little to big endian or vice versa, by directly mapping it into memory and operating on it with sequence words:"
{ $code
+ "USING: accessors grouping io.files io.mmap kernel sequences ;"
"\"mydata.dat\" dup file-info size>> ["
" 4 <sliced-groups> [ reverse-here ] change-each"
"] with-mapped-file"
}
"Send some bytes to a remote host:"
{ $code
- "\"myhost\" 1033 <inet>"
- "[ { 12 17 102 } >string write ] with-client"
+ "USING: io io.encodings.ascii io.sockets strings ;"
+ "\"myhost\" 1033 <inet> ascii"
+ "[ B{ 12 17 102 } write ] with-client"
}
{ $references
{ }