clrscr()不工作,getch()工作。为什么? [英] clrscr() not working, getch() working. Why?

查看:189
本文介绍了clrscr()不工作,getch()工作。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  #include< stdio.h> 
#include< conio.h>

int main(int argc,char const * argv []){
/ * code * /
printf(您好,按a,b或c继续) ;
char key = getch();
switch(key){
case'a':
clrscr();
//一些代码
break;
案例'b':
//多行代码
break;
case'c':
clrscr();
//多行代码
break;
默认值:
printf(Ok saliendo \\\
);
休息;
}
printf(bye);

$ / code>

getch()工作正常,但即使包含< conio.h> clrscr() p>

为什么?

解决方案

conio.h 已经死亡!

一些背景: conio.h 定义了一个API,它曾经被创建用来控制IBM PC的(text!)屏幕。它最初只是一个围绕MS-DOS函数的包装,因此您不必编写自己的程序集来创建 int 21h 来调用它们。 conio.h 的确切API从未标准化,因实现而异。



我假设你是使用针对Windows的编译器,它们通常仍然提供 conio.h 的一些变体。但是,正如你所看到的那样,并不能保证真正可用的功能和工作方式如你所愿。



现在,你甚至不得不问什么是屏幕? 控制台窗口的内容?但是,如果您的控制终端是例如远程shell(telnet,ssh,...)?甚至不同的控制台窗口实现在功能上有所不同,以及您如何控制它们。 C只知道输入和输出,它们将与任何类型的终端/控制台一起工作,因为他们不知道任何有关屏幕的内容,只是输入和输出字符。 为了实际控制屏幕,Windows提供了控制台API ,你可以直接使用它,但是你的程序只能硬连线到Windows。大多数其他控制台/终端都理解某种转义码,通常是 ANSI转义码。从Windows 10开始的Windows也有可选的支持。但是有各种各样的终端了解不同的代码(以及它们的不同子集),所以直接使用它们也不是一个好主意。




现在,控制终端/控制台的事实标准 Curses API ,它源于BSD Unix,但实现存在于各种系统和控制台中。最值得注意的是, ncurses 可用于许多系统,甚至包括Windows,但对于Windows,您也有 pdcurses 。甚至还有一个针对Windows的扩展pdcurses ,它实现了它的自己的控制台窗口,所以你可以使用本地Windows控制台没有的功能。当你使用 curses 时,你不会需要这个只是清除屏幕并从键盘上读取一些输入。

/ code>,你必须使用 curses 函数完成所有控制台/终端输入和输出(不能使用 stdio 函数,如 printf())。这里有一个小例子程序:

  #include< curses.h> 
//这里不包含`ncurses.h`,所以这个程序可以和
//不同的curses实现

#include< ctype.h>对于`isalnum()`

int main(void)
{
initscr(); //初始化诅咒,这也会清除屏幕
cbreak(); //除此之外,禁用缓冲
noecho(); //从输入

addstr(Hello,press a key!\\\
)中禁用echo字符; //输出一个常量字符串,如puts / fputs
refresh(); //输出可能被缓冲,这会强制复制到屏幕

int c;
do
{
c = getch(); //从键盘读取单个字符
} while(!isalnum(c)); //忽略任何不是字母数字的输入

printw(你输入了'%c'。'@,c); //格式化输出,如printf

addstr(按下键退出。\ n);
refresh();
c = getch();

endwin(); // exit curses
}

您可以编译它,例如使用像这样的gcc来使用 ncurses

  gcc -std = c11 -Wall -Wextra -pedantic -ocursestest cursestest.c -lncurses 

或者 pdcurses

  gcc -std = c11 -Wall -Wextra -pedantic -ocursestest cursestest.c -lpdcurses 

要详细了解 curses ,我推荐 NCURSES Programming HOWTO a>。


I'm making a small C program that asks for a key and executes some code in a switch statement.

#include <stdio.h>
#include <conio.h>

int main(int argc, char const *argv[]){
    /* code */
    printf("Hello, press a, b or c to 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() is working properly, but clrscr() is not, even if I included <conio.h>.

Why?

解决方案

conio.h is dead!

Some background: conio.h defines an API that was once created to control the (text!) screen of an IBM PC. It was originally just a wrapper around MS-DOS functions, so you didn't have to write your own assembly creating an int 21h to call them. The exact API of conio.h was never standardized and varies from implementation to implementation.

I assume you're using a compiler targeting Windows, these typically still provide some variation of conio.h. But as you can see, there's no guarantee what's really available and works as you would expect.

Nowadays, you'd even have to ask what is a screen? The content of your console window? But what if your controlling terminal is e.g. a remote shell (telnet, ssh, ...)? And even different console window implementations will differ in features and how you control them. C only knows input and output streams, they will work with any kind of terminal / console because they don't know anything about a screen, just input and output of characters.

For actually controlling the "screen", Windows provides the Console API, you could use that directly, but then your program is "hard-wired" to Windows only. Most other consoles / terminals understand some sort of escape codes, often the ANSI escape codes. Windows starting with Windows 10 has optional support for them as well. But there's a wide variety of terminals understanding different codes (and different subsets of them), so using them directly isn't a good idea either.


Nowadays, the de facto standard for controlling a terminal/console is the Curses API which has its roots in BSD Unix, but implementations exist for a large variety of systems and consoles. Most notably, ncurses is available for many systems, even including Windows, but for Windows, you also have pdcurses. There's even an extended pdcurses for Windows that implements its own console window, so you can use features the native Windows console doesn't have. Of course, you won't need this for just "clearing the screen" and reading some input from the keyboard.

When you use curses, you have to do all console/terminal input and output using curses functions (you can't use stdio functions like printf() for that). Here's a tiny example program:

#include <curses.h>
// don't include `ncurses.h` here, so this program works with 
// different curses implementations

#include <ctype.h>  // for `isalnum()`

int main(void)
{
    initscr();  // initialize curses, this also "clears" the screen
    cbreak();   // among other things, disable buffering
    noecho();   // disable "echo" of characters from input

    addstr("Hello, press a key!\n");  // output a constant string, like puts/fputs
    refresh();  // output might be buffered, this forces copy to "screen"

    int c;
    do
    {
        c = getch();        // read a single character from keyboard
    } while (!isalnum(c));  // ignore any input that's not alphanumeric

    printw("You entered '%c'.\n", c);  // formatted output, like printf

    addstr("press key to exit.\n");
    refresh();
    c = getch();

    endwin();   // exit curses
}

You can compile it e.g. with gcc like this for using ncurses:

gcc -std=c11 -Wall -Wextra -pedantic -ocursestest cursestest.c -lncurses

Or with pdcurses:

gcc -std=c11 -Wall -Wextra -pedantic -ocursestest cursestest.c -lpdcurses

To learn more about curses, I recommend the NCURSES Programming HOWTO.

这篇关于clrscr()不工作,getch()工作。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆