这个程序的代码如何编译才算完美!? (c程序员请进 )

coolness 2003-07-11 09:08:14
各位大虾,在下有个问题想请教:
问题是:

编写一个程序来实现类unix/linux下的密码输入程序( 当用户输入密码是,密码不回显 ),限用c语言,不调用库函数。


能够写出精辟的源代码给大家共享更好呀!
...全文
35 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zteliubin 2003-07-16
  • 打赏
  • 举报
回复
呵呵,好像ANSI C没有这样的函数满足需求,WIN下最好就是:

char s1[20];
p=s1;
while((ch=getch())!='\r')
{
putch('*');
*p++=ch;
}
coolness 2003-07-16
  • 打赏
  • 举报
回复
turbo c下的getch()函数是不行的,因为可移植性太差了。
而getchar()是属于asni c,但是它是显示输出呀。谁都不愿意
在输入密码的同时,密码在界面上显示出来吧。
出于这样的考虑:
1。在输出密码的同时,用*来屏蔽
2。(象linux下的一样),光标不动,所输入的密码也不回显。这种做法我觉得
比第一种还要好。
coolness 2003-07-11
  • 打赏
  • 举报
回复
1.#include <conio.h> 的可移植性很差!不能够作为通用的代码!

2.getpass () ; 没有定义;

3.用cprintf 函数 ,是否安全呢?

希望大家踊跃发言!->;
lubin59 2003-07-11
  • 打赏
  • 举报
回复
#include <conio.h>

int main(void)
{
char *password;

password = getpass("Input a password:");
cprintf("The password is: %s\r\n",
password);
return 0;
}



晨星 2003-07-11
  • 打赏
  • 举报
回复
我记得Unix下有
beep()
函数可以发警告,具体在哪个头文件中忘记了。

上面的程序是在Windows中调试的,为了使用Beep,才加了
#include <windows.h>

另外楼主所说的“不调用库函数”,我觉得没必要,总不能接收键盘输入和屏幕输出也用汇编吧?
晨星 2003-07-11
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <conio.h>
#include <windows.h>

#define ASCII_ESC 27

int getpass(char* pwd , int max)
{
char ch;
int count;

count = 0;
while(1)
{
switch(ch = getch())
{
case ASCII_ESC:
return -1;
case '\r':
pwd[count] = '\0';
return count;
break;
case '\b':
if(count > 0)
{
--count;
printf("\b \b");
}
else
Beep(2000 , 200);//BC或者TC中应该用sound。
break;
default:
if(ch >= 32 && ch <= 126) //可见字符
{
pwd[count++] = ch;
putchar('*');
if(count >= max)
{
pwd[count] = '\0';
return max;
}
}
else
Beep(2000 , 200);
break;
}
}
}

int main()
{
char s[17];
if(getpass(s , 16) >= 0)
printf("\n%s\n" , s);
return 0;
}
zteliubin 2003-07-11
  • 打赏
  • 举报
回复
getpass(char *spass)
{
char ch;
while(((ch = getchar())!='\n') //getchar()是ANSI的,应该没有问题
*spass++=ch;
}
zteliubin 2003-07-11
  • 打赏
  • 举报
回复
getpass(char *spass)
{
char ch;
while(((ch = getch())!='\n')
*spass++=ch;
}
coolness 2003-07-11
  • 打赏
  • 举报
回复
getch()的原代码好象看不到呀。。。。哪有???
1cs1ak1 2003-07-11
  • 打赏
  • 举报
回复



看看_getch()的实现,不知道哪能看到,呵呵。


24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧