我想用它来掩盖我的密码*.我使用Linux GCC代码.我知道一个解决方案是使用这样的getch()功能
#include <conio.h>
int main()
{
char c,password[10];
int i;
while( (c=getch())!= '\n');{
password[i] = c;
printf("*");
i++;
}
return 1;
}
Run Code Online (Sandbox Code Playgroud)
但问题是GCC不包含conio.h文件所以getch()对我来说没用.有没有人有办法解决吗?
是否有类似getchar能够在控制台中处理标签按下的功能?我想在我的控制台应用程序中完成某种完成.
在TurboC++中,我可以使用getch()函数conio.h.但是在Linux中,gcc没有提供conio.h.我怎样才能获得功能getch()?
我是linux编程的新手.:-)
我只是想知道我的程序(使用C语言)是否可以立即捕获linux中的每一个键击,然后决定是否回显它以及如何显示它,就像telnet一样.
例如,如果用户敲击"A",我希望程序显示"B",如果他输入"B",则需要"C",依此类推.
听起来很有趣也没用.我只是好奇.
这几天我在Ubuntu上工作。当我使用 gcc 编译 C 程序时,出现错误conio.h不存在。我想使用clrscr()和getch()发挥作用。你能告诉我这个头文件在linux中的替代品吗?
以前,我在支持#include <conio.h>头文件的Windows 上使用 c++/c 编译器,但在我拥有的 Linux 上
gcc (Debian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software...
Run Code Online (Sandbox Code Playgroud)
我想要一个与getch(). 我不知道为什么我的编译器不支持头文件#include <conio.h>
在网上搜索后,我得到了这个,它说这cin.get();可能是最接近的等价物,但这两者的不同之处在于,如果我们编写getch()它,则不会显示在控制台上输入的字符,而如果我们使用cin.get()它输入一个字符,则会在控制台上显示该字符安慰。我不希望字符显示在控制台上。
usinggetchar()还会在控制台上显示字符。
因此,对于键盘上的向上键,我得到27,令人惊讶的是,对于向下键我也得到27.我需要我的程序在向上和向下键上表现不同,我似乎无法弄明白.我正在使用Linux,需要它才能用于Linux.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
int c = getchar();
if(c==27)
{
printf("UP");
}
if(c==28)
{
printf("DOWN");
}
}
Run Code Online (Sandbox Code Playgroud) 我想在我的c程序中读取箭头按键并用其他字符串替换它们(立即在终端本身中).我正在尝试在unix终端中实现bash历史记录功能.我写了这段代码.
int
main(int argc, char *argv[])
{
char c;
char str[1024];
int i = 0;
while((c = fgetc(stdin)) != '\n'){
if(((int)c) == 27){
c=fgetc(stdin);
c=fgetc(stdin);
if (c == 'A')
{
printf("%c[A%c[2K",27, 27);
printf("UP");
}
}
str[i++] = c;
}
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为终端等待换行符或EOF将输入缓冲区发送到stdin.所以,我必须按回车键/返回键来分析用户输入.
在这个答案中,用户提到使用system ("/bin/stty raw");但这取代了所有默认的终端行为(例如退格,删除等).
那么,有没有什么方法可以直接读取/操作终端输入缓冲区并在检测到箭头按键时调整缓冲区本身?
环境 - Ubuntu(Linux)
更新1:是否有更改信号/中断的方法(默认是按输入键),使终端将存储的输入发送到缓冲区?这也可以帮助我实现相同的行为.
最终守则:
我通过检查输出来找到特定按键的ASCII字符 strace bash
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#define ESCAPE '\33'
#define BACKSPACE '\177'
#define …Run Code Online (Sandbox Code Playgroud) 我正在寻找C++代码来获取linux的getch()和getche()函数.我简要地看了一下代码,我看到他们正在使用new关键字就像一个标识符.
这是链接: 什么是Linux中的getch()和getche()相当于什么?
static struct termios old, new;
tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释它是如何可能的?它是有效的C++代码吗?谢谢.