这是我写的程序,任何人都可以告诉它有什么问题,因为无论我输入什么输入,它都会显示有效的用户。如何读取linux etc/passwd文件并比较用户输入名称以进行身份验证C
#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);
}
}
仅供参考,在将来,请选择您的代码并点击'101010'按钮进行正确格式化。 – 2010-08-25 15:53:52
谢谢我将来照顾它 – suhel 2010-08-25 15:55:44