File operations example

This example shows how to use some file operations with mov command, like opening, reading, writing, seeking and so on with the file. Example creates one file to tmpdir (whatever it may be), writes to it, reads from it, and finally deletes the file.

Compilation

Easiest way to get this example running is to go to examples directory in package root, and run:

$ ./build.sh file_operations

build.sh script compiles Ano script to C, copies source files in place and pops up instructions what to do next. Follow them. Check also examples/README for more info.

Preview

; ------------------------------------------------------------------------------
; file_operations.ano
;
; To compile:
;
; $ ./build/ano ./examples/file_operations/file_operations.ano > \
;     engine/dsl_ano.h
; ------------------------------------------------------------------------------
;
@ANO_SCRIPT_NAME		file_operations
@ANO_SCRIPT_VERSION		0.0.1
@ANO_SCRIPT_DESCRIPTION	Simple file operations example
;
@ANO_FLAGS_USE_PROTOS		[ ]
@ANO_FLAGS_VAR_NAME_SUBS	[ ]
@ANO_FLAGS_VAR_WARN_UNUSED	[ ]
;
; Copyright © 2016-2026, Jani Salonen <salojan@goto10.co>
; All rights reserved.
;
; This example shows how to use some file operations with mov command, like
; opening, reading, writing, seeking and so on with the file. Example creates
; one file to tmpdir, writes to it, reads from it, and finally deletes the
; file.
;

; Define fallback tmpdir.
;
define fallback_tmpdir ("/tmp")

main [exit: 0] {
	print "File test return values are:" . "\n\n"
	print " file does not exists / error .. 0," . "\n"
	print " unknown file .................. 1," . "\n"
	print " file is pipe or FIFO .......... 2," . "\n"
	print " character special file ........ 3," . "\n"
	print " directory ..................... 4," . "\n"
	print " block special file ............ 5," . "\n"
	print " regular file .................. 6," . "\n"
	print " symbolic link ................. 7," . "\n"
	print " socket ........................ 8, and" . "\n"
	print " whiteout ...................... 9." . "\n\n"



	; Test tmpdir directory existence…
	;
	_tmpdir = env_get("TMPDIR")

	cmp _tmpdir (NULL)
	jne "tmpdir_is_ok"
	mov _tmpdir (fallback_tmpdir)

tmpdir_is_ok:

	mov _retval ( <? _tmpdir)

	print "Testing directory '" . _tmpdir . "' existence," \
		" return value is: " . _retval . ".\n"

	; …then test that tmpdir is actually directory…
	;
	cmp _retval (4)
	je "tmp_is_ok"

	print "Testing directory '" . _tmpdir . "' failed. Abort.\n"
	exit

tmp_is_ok:

	; …and test temporary file.
	;
	mov _retval ( <? "" . _tmpdir . "/testing_example.txt")

	print "Testing file '" . _tmpdir . "/testing_example.txt' existence," \
		" return value is: " . _retval . ".\n\n"



	; Open file for read/write, create it if file does not exists.
	;
	; File open tags are:
	;
	;  <   open existing file for reading,
	;  <<  open existing file for reading/writing,
	;  >   open for writing, truncate existing file or create new,
	;  <>  open for reading/writing, truncate existing file or create new,
	;  >>  open for appending write, create if file does not exists, and
	;  <>> open for appending read/write, create if file does not exists.
	;
	;mov _filehandle ( <> "" . _tmpdir . "/testing_example.txt")
	mov _filename ("" . _tmpdir . "/testing_example.txt")
	mov _filehandle ( <> _filename)

	; Write something to file…
	;
	mov _data ("Hello, world!")
	mov _filehandle ( >- _data)
	print "Write '" . _data . "' to '" . _filename . "'.\n"

	; …and close it.
	;
	mov _retval ( <˜ _filehandle)
	print "Close file '" . _filename . "', return value is: " . \
		_retval . ".\n\n"



	; Open file again for reading.
	;
	;mov _filename ("" . _tmpdir . "/testing_example.txt")
	;mov _filehandle ( < _filename)
	mov _filehandle ( < "" . _tmpdir . "/testing_example.txt")

	; Get file name from handle…
	;
	mov _name ( <$ _filehandle)
	print "File name behind the handle is '" . _name . "'.\n"

	; …and its size.
	;
	mov _size ( <% _filehandle)
	print "File size in disk is " . _size . " bytes.\n"

	mov _size ( <# _filehandle)
	print "File content size in memory is " . _size . " bytes.\n\n"



	; Set chunk size for read…
	;
	mov _chunk_size (4)
	mov _filehandle ( >: _chunk_size)

	mov _chunk_size ( <: _filehandle)
	print "Set read chunk size, it is now " . _chunk_size . " bytes.\n"

	; …and read three chunks.
	;
	mov _data ( <- _filehandle)
	mov _addr ( <* _filehandle)
	print "Read one chunk at address " . _addr . ": '" . _data . "'\n"

	mov _data ( <- _filehandle)
	print "Read another chunk: '" . _data . "'\n"

	mov _data ( <- _filehandle)
	print "Read one more:      '" . _data . "'\n"

	mov _pos ( <! _filehandle)
	print "File position after read is: " . _pos . ".\n\n"



	; Move pos to start of file just for fun.
	;
	mov _filehandle ( <= 0)
	mov _pos ( <! _filehandle)
	print "Set file position back to begin, it is now: " . _pos . ".\n\n"



	; Delete and close the file.
	;
	mov _retval ( <˜> _filehandle)
	print "Delete and close '" . _filename . "' for good, return value" \
		" is: " . _retval . ".\n\n"



	; Finally test file existence.
	;
	mov _retval ( <? _filename)

	print "Testing file '" . _filename . "' existence," \
		" return value is: " . _retval . ".\n"
}
; ------------------------------------------------------------------------------
; file_operations.w2c
;
; To compile:
;
; $ ./build/widget ./examples/file_operations/file_operations.w2c > \
;     engine/widget_defs.h
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; file_operations.m2c
;
; To compile:
;
; $ ./build/menu ./examples/file_operations/file_operations.m2c > \
;     engine/menu_defs.h
; ------------------------------------------------------------------------------

Copyright © 2026, Jani Salonen <salojan at goto10 piste co>. Piste is finnish word and means dot. All rights reserved.