This site has been retired. For up to date information, see handbook.gnome.org or gitlab.gnome.org.


[Home] [TitleIndex] [WordIndex

Vala Input Examples

Reading a Single Line

int main () {
    stdout.printf ("Please enter your name: ");
    string? name = stdin.read_line ();
    if (name != null) {
        stdout.printf ("Hello, %s!\n", name);
    }
    return 0;
}

Reading From Standard Input (stdin)

/*
 * We use the fact that char[] allocates space
 * and that FileStream.gets takes a char[] but returns a string!
 *
 * Note that stdin.gets uses the "safe" fgets(), not the unsafe gets()
 */
string read_stdin () {
    var input = new StringBuilder ();
    var buffer = new char[1024];
    while (!stdin.eof ()) {
        string read_chunk = stdin.gets (buffer);
        if (read_chunk != null) {
            input.append (read_chunk);
        }
    }
    return input.str;
}

int main () {
    string name = read_stdin ();
    stdout.printf ("\n-----\n%s\n", name);
    return 0;
}

$ valac stdin-input.vala
$ ./stdin-input

Using GNU Readline

A comfortable way of handling user input is GNU Readline:

void main () {
    while (true) {
        var name = Readline.readline ("Please enter your name: ");
        if (name != null && name != "") {
            stdout.printf ("Hello, %s\n", name);
            Readline.History.add (name);
        } else {
            break;
        }
    }
}

$ valac readline-sample.vala --pkg readline -X -lreadline
$ ./readline-sample

Character Input

public static void main (string[] args) {
    int c = 0;
    stdout.printf ("Type something and press enter. Type '0' to quit\n");
    do {
        c = stdin.getc ();
        if (c > 10) {
            stdout.printf ("%c (%d)\n", c, c);
        }
    } while (c != '0');
}

$ valac character-input.vala
$ ./character-input

Scanf

Useful for parsing text input into numbers

public static void main() {

    float f;
    double d;
    int i;
    long l;

    stdout.printf("Enter a float   : ");
    stdin.scanf("%f", out f);

    stdout.printf("Enter a double  : ");
    stdin.scanf("%lf", out d);

    stdout.printf("Enter an integer: ");
    stdin.scanf("%d", out i);

    stdout.printf("Enter a long    : ");
    stdin.scanf("%ld", out l);

    stdout.printf("The numbers you entered\n");
    stdout.printf("Float  : %f\n",f);
    stdout.printf("Double : %lf\n",d);
    stdout.printf("Integer: %d\n",i);
    stdout.printf("Long   : %ld\n",l);

}

See the Wikipedia article on Scanf for more details. There is also a scanf function for strings.


Vala/Examples


2024-10-23 11:37