Code:
int main(void)
{
int i, len, passNum=0;
char passWord[30]={'\0'};
char **A;
char input[30];
char pass[30];
FILE *fp=fopen("passwords.txt", "r");
if(!fp)
{
printf("Error! Password file did not open\n");
return 0;
}
while((fgets(passWord, 30, fp)) != NULL)
++passNum;
printf("\n Enter Password: ");
scanf("%s",input);
strcpy(input,input);
rewind(fp);
A = malloc(passNum * sizeof(char*));
for(i=0;i<passNum;i++)
{
A[i]=malloc(30);
}
for(i=0;i<passNum;i++)
{
fgets(A[i], 30, fp);
len=strlen(A[i])-1;
if(*A[len]=='\n')
*A[len]='\0';
printf("\n current password %s ",A[i]);
printf("\n input %s ",input);
}
fclose(fp);
return 0;
}
So here is my current working code without the comparing of the user input and each passwrod retrieved from and external file. It's not perfect but after some great help from you guys its getting better. So I want to add the functionality of comparing the users input per password retrievel to see if it matches and either return 'password found' message if found somewhere in the file and 'Not found' if not.
I have been experimenting with the different ways in which to obtain user input and have gone with scanf, but I have also been told to use fgets from 'Varts' link: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com. when I use scanf as in the current example it seems to pick up the string fine, but its probably the dealing with that string that I am not understanding in terms of clearing buffers,nulling the end of the string etc. Then theres the matter of comparing the input with the passwords, which every time I try and use strcmp - it either always says match, or creates a segmentation fault. tried using strncmp to be more clinical but still failing miserably.
SO a great answer to my question would be a)How best to obtain and deal with user input. b) once the input is right, how to compare with the passwords from file.
Great Thanks