如何阅读一台linux etc / passwd文件,并用C认证比较用户的输入名称 [英] how to read a linux etc/passwd file and compare the user input name for authentication in C
本文介绍了如何阅读一台linux etc / passwd文件,并用C认证比较用户的输入名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我写谁能告诉什么是错的它的程序,因为不管输入我放弃,它显示的有效用户。
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#定义max_size个20
无效的主要()
{
FILE * FPTR;
字符的用户名[MAX_SIZE];
焦线[20];
如果((FPTR =的fopen(/ etc / passwd文件,R))== NULL)
{
的printf(不能打开文件);
}
其他
{
FPTR = FOPEN(/ etc / passwd文件,R);
的fputs(输入用户名,标准输出);
fflush(标准输出);
与fgets(用户名,用户名的sizeof,标准输入);
而((与fgets(线,的sizeof(线),FPTR))!= NULL)
{
如果(STRCMP(线,用户名))
{
的printf(%s的有效用户,用户名);
打破;
}
其他
{
的printf(%不是有效的用户,用户名);
}
}
FCLOSE(FPTR);
}
}
解决方案
你能使用的getpwent
完成这个任务?
请参阅:的getpwent(3)一>和 OpenGroup的的的getpwent
和
问候
RBO
This is the program i have written can anyone tell what is wrong with it, because whatever input i give, it shows valid user.
#include<stdio.h>
#include<string.h>
#define max_size 20
void main()
{
File *Fptr;
char username[max_size];
char line[20];
if((fptr=fopen("/etc/passwd","r"))==NULL)
{
printf("cannot open file");
}
else
{
fptr=fopen("/etc/passwd","r");
fputs("enter the username",stdout);
fflush(stdout);
fgets(username,sizeof username,stdin);
while((fgets(line,sizeof(line),fptr))!=NULL)
{
if(strcmp(line,username))
{
printf("%s valid user",username);
break;
}
else
{
printf("%s not valid user",username);
}
}
fclose(fptr);
}
}
解决方案
Could you use getpwent
for this task?
see: getpwent(3) and opengroup's getpwent
and
Regards
rbo
这篇关于如何阅读一台linux etc / passwd文件,并用C认证比较用户的输入名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文