Question

I'm programming in Vala language and I've a problem: I don't know how to read a char (although I know how to read a string and an integer).

string name = stdin.read_line();
int maximum = int.parse(stdin.read_line()); 
char option = ????;

One option is using scanf(), but if I use it I have problems during execution time.

Was it helpful?

Solution

If you just want to read a single character from a GLib.FileStream, you can use GLib.FileStream.getc, which "reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error." Actually, GLib.FileStream.read_line is implemented using getc. So, the "????" in your question would be something like (char) stdin.getc().

If you are trying to interact with a user, note that input is typically buffered by the terminal until the end of a line. If you want to get a character immediately after the user presses a key you'll need to use something like ncurses (for which Vala bindings are distributed with valac).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top