curse下面的键盘模式输入

来源:互联网 发布:网络商业计划书 编辑:程序博客网 时间:2024/05/18 06:26

代码如下:

#include <unistd.h>#include <stdlib.h>#include <curses.h>#include <string.h>#define PW_LEN 26#define NAME_LEN 256int main(){        char name[NAME_LEN];        char password[PW_LEN];        const char *real_password = "123456";        int i = 0;        initscr();        move(5,10);        printw("%s ","Please login:");        move(7,10);        printw("%s", "User name:");        getstr(name);        move(8,10);        printw("%s", "Password:");        refresh();        cbreak();        noecho();        memset(password, '\0',sizeof(password));        while( i < PW_LEN)        {                password[i] = getch();                if(password[i] == '\n') break;                move(8, 20+ i);                addch('*');                refresh();                i++;        }        echo();        nocbreak;        move(11,10);        if(strncmp(real_password, password, strlen(real_password))== 0)        printw("%s","Correct");        else        printw("%s","Wrong");        refresh();        sleep(2);        endwin();        exit(EXIT_SUCCESS);}


这里有些地方不是很清楚,自己记录一下。
initscr函数在一个程序中只能调用一次。如果成功,它返回一个指向stdscr结构的 指针;如果失败,它就输出一条诊断错误信息并使程序退出。move(row,col)是到gotoxy的宏,将光标移动到指定行row和列col.

int printw(const char *fmt, ...); 和printf一样的用法吧。

这个程序利用命令进行编译:gcc 1.c -o 1 -lcurses

执行的效果如图所示:

输入密码如果是“123456” 结果是这样的

如果不是123456结果是这样的






1 0