我怎样才能转换控制台的input,就像input密码一样

我的意思是我希望input是不可见的,比如当我loginLinux时input密码。 我怎样才能在C和Linux下实现它。 感谢名单

stty(1)在Linux中适用于我

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char buf1[100], buf2[100]; size_t len1, len2; system("stty -echo"); printf("Enter Password: "); fflush(stdout); fgets(buf1, sizeof buf1, stdin); len1 = strlen(buf1); if (len1 && buf1[len1 - 1] == '\n') buf1[--len1] = 0; puts(""); system("stty echo"); printf("Enter Password again: "); fflush(stdout); fgets(buf2, sizeof buf2, stdin); len2 = strlen(buf2); if (len2 && buf2[len2 - 1] == '\n') buf2[--len2] = 0; puts("\n\n"); printf("First password: [%s]\n", buf1); printf("Second password: [%s]\n", buf2); return 0; } 

没有单一的解决方案可以跨平台工作。

对于Linux,您可以使用getpass()函数 。

对于Windows,你可以尝试_getch() 。

据我了解,没有回声输入没有标准的输入程序。 你将不得不使用平台特定的功能。

但是,如果您正在使用GUI框架,则有些文本输入字段用于密码,并且在Windows和Linux上兼容。