Having problem with sys.io.File.update

I was trying to overwrite bytes in file. My code:

class Main {
	static function main() {
		var tst = sys.io.File.update("/home/clintflames/WORKDIR/Projects/haxe/scal/test.txt");
		tst.prepare(4);
		tst.writeBytes(haxe.io.Bytes.ofString("TEST"), 2, 4);
	}
}

test.txt:

qwertyuiop

I expected text.txt will turn into this:

qwTESTuiop

But i got error:

Uncaught exception: OutsideBounds
Called from sys.io.FileOutput.writeBytes(/usr/share/haxe/std/hl/_std/sys/io/FileOutput.hx:41)
Called from $Main.main(Main.hx:5)
Called from .init(?:1)

I think FileOutput.writeBytes() offset and length (pos, len) are for the Bytes parameter, not for the file offset, hence the exception is correct.

Try FileOutput.seek() first, and write the entire Bytes data to see if that helps

Edit:

tst.seek( 2 );
tst.writeBytes( haxe.io.Bytes.ofString("TEST"), 0, 4 );
1 Like

Thank you it helped. The only thing i can add is that FileOutput.seek(); takes second argument as sys.io.FileSeek. So i can seek from begin, end and current position.

1 Like