C中的身份验证(从给定文件读取和检查)
我已经创建了下面的代码。我需要做的是,我需要从用户那里获取和输入(用户名和密码),我需要用文件检查我输入的内容。这个问题迫使我不能一次性复制整个.txt文件。.txt文件的转换方式如下:C中的身份验证(从给定文件读取和检查),c,file,struct,linked-list,C,File,Struct,Linked List,我已经创建了下面的代码。我需要做的是,我需要从用户那里获取和输入(用户名和密码),我需要用文件检查我输入的内容。这个问题迫使我不能一次性复制整个.txt文件。.txt文件的转换方式如下: root jfl34jgf ben 12b43 stella ldfo421 kate jfd45g bill iu3556 guest 1234 test 1234 所以我想得到所有的用户名,把它们放到一个列表中,然后用输入检查每个用户名。我创建了一个结构,其中包括用户名、用户名结束的位置(使用ftell(
root
jfl34jgf
ben
12b43
stella
ldfo421
kate
jfd45g
bill
iu3556
guest
1234
test
1234
所以我想得到所有的用户名,把它们放到一个列表中,然后用输入检查每个用户名。我创建了一个结构,其中包括用户名、用户名结束的位置(使用ftell()
)和指向下一个元素的下一个指针。正如您所看到的,文本的格式是一个like是username,另一个是password。所以密码就在用户名之后。如果名称比较结果为true,则对每个用户名使用ftell(),这样我就可以检查密码检查
这是我到现在为止的代码
#包括
#包括
#包括
结构名称
{
字符名[20];
int pos;
结构名称*下一步;
};
typedef结构名称;
无效创建列表(名称*l,字符*temp1,整数位置)
{
姓名*temp;
temp=(名称*)malloc(1*sizeof(名称));
temp->next=NULL;
strcpy(temp->name,temp1);
温度->位置=位置;
温度->下一步=l;
printf(“%s”,临时->名称);
l=温度;
printf(“%s\n”,l->name);
}
无效从文件(文件*fp,名称*l)创建列表
{
char ch;
int i=0,check=0,pos;
char temp1[20];
temp1[0]='\0';
而(1)
{
如果(检查==1)
打破
而((ch=fgetc(fp))!='\n')
{
temp1[i]=ch;
i++;
}
temp1[i]='\0';
pos=ftell(fp)+1;
创建_列表(l、temp1、pos);
而((ch=fgetc(fp))!='\n')
{
如果(ch==EOF)
{
printf(“EOF”);
检查=1;
打破
}
}
i=0;
}
}
无效显示(名称*l)
{
名称*光标;
光标=l;
while(光标!=NULL)
{
printf(“%s\n”,光标->名称);
光标=光标->下一步;
}
}
int main()
{
char usern[20][20];
charpassw[20];
文件*fp;
姓名l;
l、 next=NULL;
字符c;
int i=0;
fp=fopen(“users.txt”,“r”);
如果(fp==NULL)
{
printf(“文件未打开!\n”);
出口(1);
}
从文件(fp和l)创建列表;
显示(&l);
/*fgets(usern,20,stdin);
usern[strlen(usern)-1]='\0';
while(strcmp(usern,“exit”)!=0)
{
fgets(passw,20,stdin);
passw[strlen(passw)-1]='\0';
检查(fp、usern、passw);
}
*/
返回0;
}
现在,我在链表中没有看到任何东西。我正确地从文件中获取字符串(用户名已打印),但当我尝试打印列表时,它只会给我一些奇怪的值。非常感谢您的帮助。提供的源代码在管理链接列表时出现了一个经典错误。当仅作为局部效果作为值传递时,修改函数中指针的内容,而不影响调用函数中的值 问题-修改函数
创建列表()
中名称*l
的值
函数create_list()
添加一个新节点Names*temp通过仅修改第一个节点来提高性能的列表前面的code>
void display(Names *l)
{
Names *cursor;
cursor=l->next;
while (cursor!=NULL)
{
printf("%s\n", cursor->name);
cursor=cursor->next;
}
}
- 新节点指向第一个
temp->next=l代码>,
- 然后新节点成为第一个
l=temp代码>
在本地打印值时,似乎没有问题
但是,赋值l=temp代码>仅在函数内部可用(分配给本地Names*l
指针),当从文件()返回调用函数create\u list\u时,l
不会更改
解决方案1-修改l->next
的值是最快的解决方案
在main()代码>已用赋值l声明。下一步=NULL代码>含义(列表为空)
在create_list()
函数中,赋值为:
strcpy(temp->name, temp1);
temp->pos=pos;
// assigning to the next pointer
temp->next=l->next;
printf("%s ", temp->name);
// modify the next pointer
l->next=temp;
printf("%s\n", l->next->name);
而不是:
strcpy(temp->name, temp1);
temp->pos=pos;
temp->next=l;
printf("%s ", temp->name);
l=temp;
printf("%s\n", l->name);
完整地说,对于最短的解决方案,函数display()
应从第一个节点l->next
开始
void display(Names *l)
{
Names *cursor;
cursor=l->next;
while (cursor!=NULL)
{
printf("%s\n", cursor->name);
cursor=cursor->next;
}
}
谢谢@J.Piquard为上述提供的帮助。我设法解决了这个问题。我在下面发布代码,代码可以在上面的.txt文件中测试。首先,用户输入输入,然后输入用户名和密码。假设用户名输入总是正确的,但检查用户名并不难
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct names
{
char name[20];
int pos;
struct names *next;
};
typedef struct names Names;
Names *create_list(Names *l, char *temp1, int pos)
{
Names *temp;
temp=(Names*)malloc(1*sizeof(Names));
strcpy(temp->name, temp1);
temp->pos=pos;
temp->next=l;
return temp;
}
Names * create_list_from_File(FILE *fp)
{
char ch;
int i=0, check=0, pos;
Names *l=NULL;
char temp1[20];
temp1[0]='\0';
while(1)
{
if(check==1)
break;
while((ch=fgetc(fp))!='\n')
{
temp1[i]=ch;
i++;
}
temp1[i]='\0';
pos=ftell(fp)+1;
l=create_list(l,temp1, pos);
while((ch=fgetc(fp))!='\n')
{
if(ch==EOF)
{
check=1;
break;
}
}
i=0;
}
return l;
}
int check_usrn(char *s, Names *l, int *i)
{
int flag=0;
Names *cursor=l;
*i=0;
while (cursor!=NULL)
{
if(strcmp(cursor->name, s)==0)
{
flag=1;
break;
}
cursor=cursor->next;
(*i)++;
}
return flag;
}
int check_passw(FILE *fp, Names *l, char *passw, int *i)
{
Names * cursor;
cursor=l;
int m=0, flag=0;
char c, temp[20];
while(m!=*i)
{
cursor=cursor->next;
m++;
}
m=0;
fseek(fp, cursor->pos-1, SEEK_SET);
while((c=fgetc(fp))!='\n')
{
if(c==EOF)
break;
temp[m]=c;
m++;
}
temp[m]='\0';
if(strcmp(passw, temp)==0)
flag=1;
return flag;
}
int main()
{
char usern[20];
char passw[20];
char file_name[20];
FILE *fp;
Names *l=NULL;
int i=0;
fgets(file_name, 20, stdin);
file_name[strlen(file_name)-1]='\0';
fp=fopen(file_name, "r");
if(fp==NULL)
{
printf("File not opened!\n");
exit(1);
}
l=create_list_from_File(fp);
while(strcmp(usern, "exit")!=0)
{
fgets(usern, 20, stdin);
usern[strlen(usern)-1]='\0';
if(check_usrn(usern, l, &i)==1)
{
fgets(passw, 20, stdin);
passw[strlen(passw)-1]='\0';
if(check_passw(fp ,l, passw, &i)==1)
printf("Access to user %s is granted.\n", usern);
else
printf("Access to user %s is denied.\n", usern);
}
}
printf("Exiting ...\n");
fclose(fp);
return 0;
}
#包括
#包括
#包括
结构名称
{
字符名[20];
int pos;
结构名称*下一步;
};
typedef结构名称;
名称*创建列表(名称*l、字符*temp1、整数位置)
{
姓名*temp;
temp=(名称*)malloc(1*sizeof(名称));
strcpy(temp->name,temp1);
温度->位置=位置;
温度->下一步=l;
返回温度;
}
名称*从文件创建列表(文件*fp)
{
char ch;
int i=0,check=0,pos;
名称*l=NULL;
char temp1[20];
temp1[0]='\0';
而(1)
{
如果(检查==1)
打破
而((ch=fgetc(fp))!='\n')
{
temp1[i]=ch;
i++;
}
temp1[i]='\0';
pos=ftell(fp)+1;
l=创建列表(l、temp1、pos);
而((ch=fgetc(fp))!='\n')
{
如果(ch==EOF)
{
检查=1;
打破
}
}
i=0;
}
返回l;
}
整数校验(字符*s,名称*l,整数*i)
{
int标志=0;
名称*光标=l;
*i=0;
while(光标!=NULL)
{
如果(strcmp(光标->名称,s)==0)
{
flag=1;
打破
}
光标=光标->下一步;
(*i)++;
}
返回标志;
}
int check_passw(文件*fp,名称*l,字符*passw,int*i)
{
名称*光标;
光标=l;
我