Haxe Code Cookbook
Haxe programming cookbookBeginnerstdin, stdout, stderr

stdin, stdout, stderr

Reading time: 0.5 minute

Reading from stdin and writing to stdout and stderr.

stdin

You can read from stdin interactively from the command line, or can pipe input to your Haxe program like you would with any other command line utility.

To read in one line:

Sys.println("Enter your name:");
var ans = Sys.stdin().readLine();
// `ans` is just the text --- no newline

If you want to iteratively read in lines:

var line:String;
var lines:Array<String> = [];
try {
    while (true) {
        line = Sys.stdin().readLine();
        lines.push(line);
    }
}
catch (e:haxe.io.Eof) {
    trace("done!");
}

You could also read in all the input in one shot:

var content = Sys.stdin().readAll().toString();

stdout

There's a few ways to write to stdout:

trace("Hello, trace!");
Sys.println("Hello, println!");
Sys.print("Hello, print!"); // no added newline

You can also use Sys.stdout() to grab the stdout object and call its write methods (see haxe.io.Output).

stderr

To write to stderr:

Sys.stderr().writeString("Yow!\n");

Contributors:
John Gabriele
Last modified:
Created:
Category:  Beginner
Tags: