c - clrscr() not working, getch() working. Why? -
i'm making small c
program asks key , executes code in switch statement.
#include <stdio.h> #include <conio.h> int main(int argc, char const *argv[]){ /* code */ printf("hello, press a, b or c continue"); char key = getch(); switch (key){ case 'a': clrscr(); //some code break; case 'b': //many lines of code break; case 'c': clrscr(); //many lines of code break; default: printf("ok saliendo\n"); break; } printf("bye"); }
getch()
working properly, clrscr()
not, if included <conio.h>
.
why?
conio.h
dead!
some background: conio.h
defines api once created control (text!) screen of ibm pc. wrapper around ms-dos functions, didn't have write own assembly creating int 21h
call them. exact api of conio.h
never standardized , varies implementation implementation.
i assume you're using compiler targeting windows, these typically still provide variation of conio.h
. can see, there's no guarantee what's available , works expect.
nowadays, you'd have ask what screen? content of console window? if controlling terminal e.g. remote shell (telnet, ssh, ...)? , different console window implementations differ in features , how control them. c knows input , output streams, work kind of terminal / console because don't know screen, input , output of characters.
for controlling "screen", windows provides console api, use directly, program "hard-wired" windows only. other consoles / terminals understand sort of escape codes, ansi escape codes. windows starting windows 10 has optional support them well. there's wide variety of terminals understanding different codes (and different subsets of them), using them directly isn't idea either.
nowadays, de facto standard controlling terminal/console curses api has roots in bsd unix, implementations exist large variety of systems , consoles. notably, ncurses available many systems, including windows, windows, have pdcurses. there's extended pdcurses windows implements own console window, can use features native windows console doesn't have. of course, won't need "clearing screen" , reading input keyboard.
when use curses
, have console/terminal input , output using curses
functions (you can't use stdio
functions printf()
that). here's tiny example program:
#include <curses.h> // don't include `ncurses.h` here, program works // different curses implementations #include <ctype.h> // `isalnum()` int main(void) { initscr(); // initialize curses, "clears" screen cbreak(); // among other things, disable buffering noecho(); // disable "echo" of characters input addstr("hello, press key!\n"); // output constant string, puts/fputs refresh(); // output might buffered, forces copy "screen" int c; { c = getch(); // read single character keyboard } while (!isalnum(c)); // ignore input that's not alphanumeric printw("you entered '%c'.\n", c); // formatted output, printf addstr("press key exit.\n"); refresh(); c = getch(); endwin(); // exit curses }
you can compile e.g. gcc using ncurses
:
gcc -std=c11 -wall -wextra -pedantic -ocursestest cursestest.c -lncurses
or pdcurses
:
gcc -std=c11 -wall -wextra -pedantic -ocursestest cursestest.c -lpdcurses
to learn more curses
, recommend ncurses programming howto.
wiki
Comments
Post a Comment