这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define m 9
int flag1=1;
//Creating memory for username and password
struct node // The list that is being used to store username and password
{
char username[30];
char password[30];
struct node *next;
};
int getkey(char[30]); //a method to get "randomly" a key
int hash(int); // a method to create the hashcode
void insert(struct node **,char[30],char[30]); //adding a node struct to a specific possition in hashtable
void initializeHashTable(struct node **);
void search(struct node **,char[30]);//check if password exists
void searchuser(struct node **,char[30]); //check if username exists
void print(struct node**);//printing the hashtable
void print(struct node **T)
{
struct node *tmp;
int i,flag=0;
printf("-------Data Base-------\n");
for(i=0;i<m;i++)
{
tmp=*(T+i);
while(tmp!=NULL)
{
printf("Username: %s Password: %s\n",tmp->username,tmp->password);
tmp=tmp->next;
}
}
}
void search(struct node **T,char password[30])
{
struct node *tmp;
int i,flag=0;
for(i=0; i<m; i++)
{
tmp=*(T+i);
while(tmp!=NULL)
{
if(strcmp(tmp->password,password)==0)
{
flag=1;
break;
}
tmp=tmp->next;
}
if(flag==1)
break;
}
if(flag==1)
printf("Authentication Successfull\n");
else
printf("Authentication Failed\n");
}
void searchuser(struct node **T,char user[30])
{
struct node *tmp;
int i,flag=0;
for(i=0; i<m; i++)
{
tmp=*(T+i);
while(tmp!=NULL)
{
if(strcmp(tmp->username,user)==0)
{
flag=1;
break;
}
tmp=tmp->next;
}
if(flag==1)
break;
}
if(flag==0)
{
printf("Username NOT Found\n");
flag1=0;
}
}
void initializeHashTable(struct node **T)
{
int i;
for (i=0; i<m; i++)
*(T+i)=NULL;
}
int getkey(char name[30])
{
int i=0;
int key=0;
for (i=0; i<15; i++)
{
key+=name[i];
}
return key;
}
int hash(int key)
{
return key%m;
}
void insert (struct node **T,char name[30],char password[30])
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
strcpy(newnode->username,name);
strcpy(newnode->password,password);
int key;
key=getkey(name);
int hashcode;
hashcode=hash(key);
//printf("Key:%d\n",key);
// printf("Hashcode:%d\n",hashcode);
if(*T+hashcode==NULL)
{
(*(T+hashcode))->next=newnode;
newnode->next=NULL;
}
else
{
newnode->next=(*(T+hashcode));
(*(T+hashcode))=newnode;
}
}
int main()
{//possible content of file: phill 1234
char choice;
struct node **T;
struct node **head;
head==NULL;
T=(struct node**)malloc(m*sizeof(struct node));//creating the hashmap
FILE *fp;
char name[30];
char pass[30];
fp=fopen("passwords.txt","r");
if (fp==NULL)
{
printf("File Not Found");
return 0;
}
else
{
initializeHashTable(&(*T));
while(!feof(fp)) // end of file
{
fscanf(fp,"%s %s",name,pass); //reads until first wild space,
//one each loop, you get 1 username and 1 password from the file
//adding them on hashmap
insert(&(*T),name,pass);
}
}
//user authentication
do{
printf("Enter Username:\n");
fflush(stdin);
scanf("%s",&name);
searchuser(&(*T),name);
if(flag1==1)
{
printf("Enter Password:\n");
scanf("%s",&pass);
search(&(*T),pass);
}
printf("Try Again? (y/n)\n");
fflush(stdin);
scanf("%d",&choice);
}while(choice!='y');
free(T);
free(fp);
free(head);
}
成功但有时它会崩溃,或者我给出相同的
详细信息(用户名和密码)以及身份验证失败
//user authentication
do{
printf("Enter Username:\n");
fflush(stdin);
scanf("%s",&name);
searchuser(&(*T),name);
if(flag1==1)
{
printf("Enter Password:\n");
scanf("%s",&pass);
search(&(*T),pass);
}
printf("Try Again? (y/n)\n");
fflush(stdin);
scanf("%d",&choice);
}while(choice!='y');
如果我把
do..while(choice=='y')
这意味着运行循环 while choice=='y'
它停止了if choice=='y'
这有点奇怪Issue: Authenticator doesn't work properly after 1st try
非常感谢您的时间!
最佳答案
几个问题让我想到了:
struct node **T;
...
T=(struct node**)malloc(m*sizeof(struct node));
你打算为
T
成为 struct node
的序列(在这种情况下 T
的类型是错误的),或者指向 struct node
的指针序列(在这种情况下,sizeof
的论点是错误的)?如果你写的话,它会更干净(而且更不容易出错)
T = malloc( m * sizeof *T );
那么这只是决定
T
的类型的问题。 .就个人而言,我希望您分配 struct node
的序列。 , 所以很可能会被宣布struct node *T = malloc( m * sizeof *T );
但是,您的其余代码似乎确实假设
T
是指向 struct node
的指针序列,所以在那种情况下,那将是struct node **T = malloc( m * sizeof *T );
这就是这个成语的美妙之处——你唯一需要改变的是
T
的类型。 . malloc
call 本身不需要改变。如果 T
的类型是 struct node *
,然后 sizeof *T
相当于sizeof (struct node)
.如果 T
是 struct node **
,然后 sizeof *T
相当于sizeof (struct node *)
.然后是这个:
initializeHashTable(&(*T));
这真的应该是
intializeHashTable( T );
还,
while(!feof(fp))
总是错的 -
feof
直到您尝试读取文件末尾之后才会返回 true,因此您会经常循环一次。您应该检查输入操作的结果:while ( fscanf(fp,"%s %s",name,pass) == 2 )
{
insert(T,name,pass);
}
同样,论点
&(*T)
应该只是 T
.至于你的输入:
printf("Enter Username:\n");
fflush(stdin);
scanf("%s",&name);
fflush
这里不需要调用 - 如果在输出操作之后立即有输入操作,则暗示刷新。编辑
不好意思说我看错了
fflush
调用 - 出于某种原因,我将其读作 fflush( stdout)
,这意味着您要确保在调用 scanf
之前将输出写入控制台.调用
fflush
在输入流上是错误的,这样做的行为是未定义的。它不会清除任何未读输入的输入流(MSVC 除外,这是因为 Microsoft 决定对现有的不良行为进行编码)。失去
fflush
完全调用。结束编辑
scanf
电话应该是scanf( "%s", name );
&
这里不需要 - name
将从“char
的 30 元素数组”类型隐式转换为“指向 char
的指针”。 pass
相同.编辑
正如所写,这些
scanf
调用是不安全的 - 如果您输入的字符串比目标缓冲区长,scanf
会很高兴地将这些额外的字符写入该缓冲区之后的内存中,从而导致从损坏的数据到段错误的任何事情。你需要确保你没有读到太多的字符。您可以使用字段宽度说明符来执行此操作,例如scanf( "%29s", name ); // leave at least one element for the 0 terminator
或使用
fgets
:fgets( name, sizeof name, stdin );
fgets
如果有空间, call 将读取尾随换行符并将其存储到缓冲区中,因此您需要做一些额外的工作:char *newline = strchr( name, '\n' );
if ( *newline )
*newline = 0;
如果缓冲区中没有换行符,那么您将需要在下一次读取之前清除输入流:
while ( getchar() != '\n' )
; // empty loop
结束编辑
scanf("%d",&choice);
}while(choice!='y');
您使用了错误的格式说明符来读取
choice
- 你在告诉 scanf
期望十进制整数输入,而不是字符。 'y'
与格式不匹配,所以 scanf
实际上并没有从输入流中读取它,并且 choice
没有更新。你应该把它改成 choice = getchar();
} while ( choice != 'y' );
还有很多其他问题,但从这些开始。
关于C - 哈希表无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44145235/