Skip to content
Tom Brasington edited this page Jan 6, 2014 · 2 revisions

Higgs has a library to provide common I/O functionality. It is a wrapper around the C I/O functions found in the stdio library.

Importing:

// To use the I/O functions you must first import the stdio module:
var io = require('lib/stdio');

File operations:

// The stdio module provides some common file operations

// Delete a file, returns true if successful or false otherwise
io.remove('test.txt');

// Rename a file, returns true if successful or false otherwise
io.rename('foo.txt', 'bar.txt');

// Get a temporary file, returns a File object on success or throws error on failure
var myfile = io.tmpfile();

// Get a unique name, returns a string containing the name on success or throws error on failure
var name = io.tmpname();

// Open a file, returns a File object on success or throws error on failure
// open foo.txt for reading
var foo = io.fopen("foo.txt", "r");

File objects:

// Some functions like fopen return an instance of File:
var myfile = io.fopen("foo.txt", "w+");

// File objects provide methods for reading, writing, etc

// Get a character, will return "" for EOF
var c = myfile.getc();

// Read a line, including newline
var line = myfile.readLine();

// You can also specify a maximum length to read
// the next read will get the remainder of the line (if any)
var first_ten_chars = myfile.readLine(10);
var remainder = myfile.readLine();

// Read the next x chars
var ten_chars = myfile.read(10);

// Read the entire file
var test = myfile.read();

// There are functions for reading binary data:
var answer = file.readUint8();
var steps = file.readInt8();
var spartans = file.readUint16();
var years = file.readInt16();
var jnumber = file.readUint32();
var population = file.readFloat64();

// Write a char to the file, returns true if successful or false otherwise
myfile.putc("H");

// Write a string to the file, returns true if successful or false otherwise
myfile.write("Hello World.");

// There are functions for writing binary data:
file.writeUint8(42);
file.writeInt8(8);
file.writeUint16(300);
file.writeInt16(100);
file.writeUint32(8675309);
file.writeFloat64(1.536);

// Flush a file, returns true if successful or false otherwise
myfile.flush();

// Returns true if the next character is EOF
myfile.EOF();

// Returns the current position in the file
myfile.tell();

// Rewind to beginning of file
myfile.rewind();

// Seek to 10th character
myfile.seek(10);

// Seek 2 characters ahead
myfile.seek(2, io.SEEK_CUR);

// When finished with a file close() will close it and free some resources
myfile.close();

The stdin/stdout/stderr streams:

// The stdio module provides File objects for stdin/stdout/stderr

// Get a char from stdin
io.stdin.getc();

// Write to stdout
io.stdout.write("Hello!");

// Write to stderr
io.stderr.write("Hello!");

Notes:

  • There is currently no support in stdio for wchar functions.
  • Functions in the stdio library must be called in the context of the stdio module. var fopen = io.fopen will not work.
Clone this wiki locally