在C中,我想显示用户输入的每个字符* (例如,请输入您的密码:*****)
我在寻找,但无法为此找到解决方案。 我正在研究Ubuntu。有人知道一个好方法吗?
答案 0 :(得分:3)
查看ncurses库。它是一个非常自由许可的库,在各种系统上都具有大量功能。我没有太多使用它,所以我不确定你想要调用哪些功能,但如果看一下documentation,我相信你会找到你想要的。
答案 1 :(得分:1)
查看我的代码。它适用于我的FC9 x86_64系统:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
int main(int argc, char **argv)
{
char passwd[16];
char *in = passwd;
struct termios tty_orig;
char c;
tcgetattr( STDIN_FILENO, &tty_orig );
struct termios tty_work = tty_orig;
puts("Please input password:");
tty_work.c_lflag &= ~( ECHO | ICANON ); // | ISIG );
tty_work.c_cc[ VMIN ] = 1;
tty_work.c_cc[ VTIME ] = 0;
tcsetattr( STDIN_FILENO, TCSAFLUSH, &tty_work );
while (1) {
if (read(STDIN_FILENO, &c, sizeof c) > 0) {
if ('\n' == c) {
break;
}
*in++ = c;
write(STDOUT_FILENO, "*", 1);
}
}
tcsetattr( STDIN_FILENO, TCSAFLUSH, &tty_orig );
*in = '\0';
fputc('\n', stdout);
// if you want to see the result:
// printf("Got password: %s\n", passwd);
return 0;
}
答案 2 :(得分:0)
手动完成;使用例如conio中的getch()一次读取一个字符输入,并为每个字符打印*。
答案 3 :(得分:0)
使用像这样的程序 问我更多问题
此程序用于放置*而不是char,并在使用退格后删除输入^^
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
char a[100],c;
int i;
fflush(stdin);
for ( i = 0 ; i<100 ; i++ )
{
fflush(stdin);
c = getch();
a[i] = c;
if ( a[i] == '\b')
{
printf("\b \b");
i-= 2;
continue;
}
if ( a[i] == ' ' || a[i] == '\r' )
printf(" ");
else
printf("*");
if ( a[i]=='\r')
break;
}
a[i]='\0';
printf("\n%s" , a);
}
答案 4 :(得分:0)
这是最简单的程序,仅使用头文件stdio.h和conio.h在C编程语言中的Character位置实现星号。
#include<stdio.h>
#include<conio.h>
void main()
{
char pwd[15];
int i;
printf("Enter Password : ");
for(i=0;i<15;i++)
{
pwd[i]=getch();
if(pwd[i]!='\r')
{
printf("*");
}
if(pwd[i]==13)
break;
}
printf("\n \nPassword is : ");
for(i=0;i<15;i++)
printf("%d ",pwd[i]);
}