-1

The strcmp() function is not working. The username I am reading from console doesn't match the one i Have in the file. So, the output is wrong id. What is the solution?

  #include<stdio.h>       
  #include<stdlib.h>        
  #include<string.h>       
  int main()         
  {    
   FILE *pass;     
   char str[100],str2[100];     
   char id[100],pw[100];        
   pass=fopen("password.txt","r");         
   printf("ENTER USER NAME AND PASSWORD:\n");        
   printf("USERNAME: ");       
   gets(id);            
   printf("\nPASSWORD: ");         
   gets(pw);       
   while(fgets(str,100,pass)!=NULL);            
   {        
    fgets(str2,100,pass);      
    if(strcmp(id,str)==0)       
    {       
      if(strcmp(pw,str2)==0)         
      {       
        printf("ACCEPTED\n");            
      }       
      else       
      {        
        printf("wrong password");          
       }        
   }        
   else         
    {          
      printf("wrong id");           
    }           
    }    
    fclose(pass);    
   }          
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

2 Answers2

2

First of all, never use gets(), use fgets() always.

That said, fgets(), reads and stores the trailing newline into the target buffer.

From C11, chapter 7.21.7.2

The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.

You should get rid of that newline before making use of the scanned value. Here's one way to have that accomplished.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

You should check if the fopen() was successful before accessing the file that the program attempted to open.

fopen() returns NULL on error. So do something like

pass=fopen("password.txt","r");         
if( pass==NULL )
{
    perror("Unable to open input file.");       
}
else
{
    // Use the file 
}

And gets() is known to severe security concerns. See this.

It is advisable to use a fgets() instead with stdin as an argument. So instead of

gets(id);            

try

if( fgets(id, sizeof(id), stdin)!=NULL )
{
    id[ strlen(id)-1 ] = '\0';
    // Use `id` 
}   

The value returned by fgets() is also checked to see that fgets() was successful. It returns NULL on error.

It should be noted that fgets() would read in the trailing \n from stdin as well.

You may remove this extra \n with the help of a function like strlen(). See a discussion on removing the extra newline here.

Use the same while reading into pw as well.

Assuming that your input file has the id in one line and the corresponding password in the line after that, you could do something like

char flag=0;
while(fgets(str, sizeof str, pass)!=NULL && fgets(str2, sizeof str2, pass)!=NULL)
{
    str[ strlen(str)-1 ] = '\0';
    str2[ strlen(str2)-1 ] = '\0';
    if( strcmp(str, id)==0 )
    {
        flag=1;
        if( strcmp(str2, pw)==0 )
        {
            printf("ACCEPTED\n");            
        }
        else 
        {
            printf("wrong password.");
        }
        break;
    }
}
if(flag==0) 
{
    printf("Wrong id.");    
}

Read two lines at once and check if both are successful before each iteration of the loop to ensure that proper values have been read.

You may want to break out of the loop once you find the password to be wrong.

You could also use a flag variable to see if the id that was input was valid and print the corresponding message at the end.

J...S
  • 5,079
  • 1
  • 20
  • 35