【问题标题】:subsititue of getchar and clrscr() in c for linux在 c for linux 中替代 getch 和 clrscr()
【发布时间】:2012-08-06 07:11:58
【问题描述】:

这些天我在 Ubuntu 上工作。当我使用 gcc 编译我的 C 程序时,它给出了错误 conio.h 不存在。 我想使用clrscr()getch() 函数。 你能告诉我这个头文件在linux中的替代品吗?

【问题讨论】:

标签: c linux


【解决方案1】:

getch() 函数可以在curses.h(库“curses”)中找到。同一个库提供了清除屏幕的功能。查看这些链接:

http://linux.die.net/man/3/getch

http://linux.die.net/man/3/erase

【讨论】:

  • 我相信 curses getch() 函数只有在调用 initscr() 之后才能使用,它控制了(文本)屏幕。
【解决方案2】:

system("clear"); 可以在 linux 中使用,而不是 clrscr();

【讨论】:

    【解决方案3】:
    # include <curses.h>
    
           int erase(void);
           int werase(WINDOW *win);
           int clear(void);
           int wclear(WINDOW *win);
           int clrtobot(void);
           int wclrtobot(WINDOW *win);
           int clrtoeol(void);
           int wclrtoeol(WINDOW *win);
    
    DESCRIPTION
           The erase and werase routines copy blanks to every position in
           the window, clearing the screen.
    

    我猜这个问题一再被否决,因为它意味着对基本 C 语言功能的理解不足和/或 OP 只是将代码复制/粘贴到编辑器/IDE 中。

    同样,只需在您的代码中使用system("exit");

    #include<stdlib.h>
    main()
    {
    system("clear"); //clears the screen
    } 
    

    查看手册页显示:

    SYSTEM(3)                                 Linux Programmer's Manual   SYSTEM(3)
    
    NAME
           system - execute a shell command
    
    SYNOPSIS
           #include <stdlib.h>
    
           int system(const char *command);
    
    DESCRIPTION
           system() executes a command specified in command by calling /bin/sh -c 
    command, and returns after the command has been completed.  
    During execution of the command, SIGCHLD will be blocked, and SIGINT
    and SIGQUIT will be ignored.
    

    这个问题也可能与以下问题重复:

    最后,看看下面的更多细节和例子:

    【讨论】:

      【解决方案4】:

      显然你没有尝试谷歌搜索。

      没有直接的选择。

      这篇博文:http://wesley.vidiqatch.org/code-snippets/alternative-for-getch-and-getche-on-linux/ 为您提供getch()getche() 的替代方案

      您也可以使用 libncurses 做您想做的事:http://tech.dir.groups.yahoo.com/group/linux/message/29221

      【讨论】:

      • 我正在尝试 curses.h,但在我的 ubantu 操作系统上也找不到它
      • 您是否尝试在存储库中找到它?如果您不知道如何操作,请尝试 askubuntu.com。
      【解决方案5】:

      curses.h 是 conio.h 的替代品。 安装 build-essentials 并安装 libncurses5-dev。

      然后您可以使用这些功能。 [http://ubuntuforums.org/showthread.php?t=880601][1]

      【讨论】:

        【解决方案6】:

        我正在修改一些代码;安装 ncurses 后,我插入了这些代码:

        #include <stdio.h>
        #include <ncurses.h>
        
        main ()
        {
        
        system ("clear");
        getchar ();
        
        }
        

        【讨论】:

          【解决方案7】:

          还有另一种方法可以通过 C 代码而不是系统调用来实现。

          void clrscr(void) {
            fprintf(stdout, "\033[2J\033[0;0f");
            fflush(stdout);
          }
          

          很久以前就找到了,已经在raspbian上检查成功了。

          还有:

          void gotoxy(int x, int y) {
            printf("%c[%d;%df",0x1B, y, x);
          }
          

          希望对你有帮助。

          问候。

          【讨论】:

            【解决方案8】:

            在G++编译器中,我们使用stdlib.h头文件中定义的system("clear")函数

            #include<iostream>
            #include<stdlib.h>
            
            int main() {
              std::cout<<"Hello Aliens:";
              system("clear");
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-06-02
              相关资源
              最近更新 更多