Hare by Example: Writing Files

Writing files are just as easy as reading them. We can use the same modules, but use the write functions instead. Since we're using random data the output of your execution will differ, but the number of bytes written will be the same.

use bufio;
use crypto::random;
use encoding::hex;
use io;
use fmt;
use fs;
use os;
use strings;

export fn main() void = {
	// We create a brand new file in a temporary location. We also
	// specify appropriate permissions from the fs::mode enum.
	const handle = os::create("/tmp/writing-files.txt",
		fs::mode::USER_RW)!;

	const data: []u8 = strings::toutf8("hello\nhare\n");
	io::write(handle, data)!;

	const nonascii: []u8 = [2, 233, 10, 113, 13];
	io::write(handle, nonascii)!;

	// Set up read and write buffers, but since we're only writing
	// our read buffer is just an empty slice.
	let rbuf: []u8 = [];
	let wbuf: [os::BUFSZ]u8 = [0...];
	const buf = bufio::init(handle, rbuf, wbuf);

	// This will set the write buffer to an empty slice when we
	// flush all current writes to the underlying handle of the
	// buffer. In our case here, it is the file handle.
	bufio::setflush(&buf, []);

	// Generate a bunch of random bytes to write with.
	let rand: [100]u8 = [0...];
	random::buffer(rand[..]);

	io::write(&buf, rand[..])!;
	io::write(&buf, rand[..])!;
	io::write(&buf, rand[..])!;

	// Let's flush all those writes to the file. If we do not do
	// this and our program exits the data will be lost since it is
	// only in memory at this point.
	bufio::flush(&buf)!;

	// Let's close the file so we can open it and dump the contents.
	io::close(handle)!;

	const reader = os::open("/tmp/writing-files.txt")!;
	defer io::close(reader)!;

	// Set up our normal read buffer.
	let buf: [os::BUFSZ]u8 = [0...];

	// Force read to return the bytes written. We panic on error
	// with ! and ignore the EOF with ensuring we get a size back.
	const sz = io::read(reader, buf)! as size;

	fmt::println("Bytes read", sz)!;

	// We just dump the file contents as hex to see its contents.
	hex::dump(os::stdout, buf[..sz])!;
};
$ hare run writing-files.ha
4/4 tasks completed (100%)
Bytes read 316
00000000  68 65 6c 6c 6f 0a 68 61  72 65 0a 02 e9 0a 71 0d  |hello.hare....q.|
00000010  92 2a a9 e1 90 59 30 9e  d1 c2 27 80 1b be 3d 35  |.*...Y0...'...=5|
00000020  cf 5a 91 dc 71 4a e1 f8  3c 57 41 70 7c 27 bd 5e  |.Z..qJ..<WAp|'.^|
00000030  0e 47 96 cd 2c e5 48 13  b6 d8 09 dc 08 90 23 a3  |.G..,.H.......#.|
00000040  26 7a 65 f6 7c d3 0b f4  31 94 9b 66 9b c4 f3 29  |&ze.|...1..f...)|
00000050  73 37 6b 30 e2 f0 1f 12  d5 75 78 2a 74 82 91 0e  |s7k0.....ux*t...|
00000060  b1 51 ed af 3b 31 cd b2  d0 05 c7 aa 93 a8 ac b4  |.Q..;1..........|
00000070  35 75 ae d6 92 2a a9 e1  90 59 30 9e d1 c2 27 80  |5u...*...Y0...'.|
00000080  1b be 3d 35 cf 5a 91 dc  71 4a e1 f8 3c 57 41 70  |..=5.Z..qJ..<WAp|
00000090  7c 27 bd 5e 0e 47 96 cd  2c e5 48 13 b6 d8 09 dc  ||'.^.G..,.H.....|
000000a0  08 90 23 a3 26 7a 65 f6  7c d3 0b f4 31 94 9b 66  |..#.&ze.|...1..f|
000000b0  9b c4 f3 29 73 37 6b 30  e2 f0 1f 12 d5 75 78 2a  |...)s7k0.....ux*|
000000c0  74 82 91 0e b1 51 ed af  3b 31 cd b2 d0 05 c7 aa  |t....Q..;1......|
000000d0  93 a8 ac b4 35 75 ae d6  92 2a a9 e1 90 59 30 9e  |....5u...*...Y0.|
000000e0  d1 c2 27 80 1b be 3d 35  cf 5a 91 dc 71 4a e1 f8  |..'...=5.Z..qJ..|
000000f0  3c 57 41 70 7c 27 bd 5e  0e 47 96 cd 2c e5 48 13  |<WAp|'.^.G..,.H.|
00000100  b6 d8 09 dc 08 90 23 a3  26 7a 65 f6 7c d3 0b f4  |......#.&ze.|...|
00000110  31 94 9b 66 9b c4 f3 29  73 37 6b 30 e2 f0 1f 12  |1..f...)s7k0....|
00000120  d5 75 78 2a 74 82 91 0e  b1 51 ed af 3b 31 cd b2  |.ux*t....Q..;1..|
00000130  d0 05 c7 aa 93 a8 ac b4  35 75 ae d6              |........5u..|