Click here to Skip to main content
15,889,499 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code, I want to check input and text in a file to be matched and if it is matched, it should give me a message of success and print the text, otherwise deny the access.

int main(){
  FILE * fr = fopen("fiile.csv","r");
  char str[50], string[50];
  short int FLAG = 0;

  printf("Enter your name: ");
  fgets(str, sizeof(str), stdin);

  while (fgets(string, 49, fr)){
    if (strncmp(str, string, strlen(str)) == 0){
      printf("Here is your details");
      printf("%s", string);
      FLAG = 1;
    }
  }

  if (!(FLAG == 1)){
    printf("Access denied.\n");
    return -1;
  }
  fclose(fr);

  return 0;
}


What I have tried:

In this code, I want to check input and text in a file to be matched and if it is matched, it should give me a message of success and print the text, otherwise deny the access.
Posted
Updated 20-Aug-20 11:17am
Comments
Sandeep Mewara 20-Aug-20 14:29pm    
It would help you if you share where are you stuck while trying?

1 solution

If you are matching user names and passwords then you do not want to use strncmp. You should make sure they match exactly. You might find that when reading the input leaves new line characters or carriage returns in the buffer. If this is the case, I find it often is, then write yourself a function to remove them. Here is one way you can do it that is not particularly high performance :
C++
void RemoveNewLines( char * buffer )
{
   char * ptr;
   ptr = strchr( buffer, '\n' );
   if( ptr )
       * ptr = 0;
   ptr = strchr( buffer, '\r' );   // in case you see carriage returns also
   if( ptr )
       * ptr = 0;
}
You can then revise your code to look like this :
C++
#define STRING_SIZE  49

int main()
{
  char instr[STRING_SIZE+1];
  char string[STRING_SIZE+1];
  FILE * fr = NULL;
  int flag = 0;

  fr = fopen("fiile.csv","r");
  if( fr == NULL )
  {
     printf( "Unable to open file\n" );
     return 1;
  }

  printf("Enter your name: ");
  fgets( instr, STRING_SIZE, stdin);
  RemoveNewLines( instr );

  while( fgets( string, STRING_SIZE, fr ) )
  {
     RemoveNewLines( string );
     if( strcmp( instr, string ) == 0 )
     {
      printf("Here is your details");
      printf("%s", string);
      flag = 1;
      break;
    }
  }

  fclose(fr);

  if( flag != 1 )
  {
    printf("Access denied.\n");
    return -1;
  }

  return 0;
}
I think I see a potential problem with this. If your file is really a .CSV and you have other items on the same line (the details) then this will never match. However you can't just do a limited match. The two names "Jon" and "Jonathan" could possibly match if done on a limited basis so that is a problem.

To work around that issue, assuming the user name is followed by a comma in the file, you can try appending a comma to the user's entry. Then that would force the two to match up to the comma which should provide an exact match of user names. That revision could look like this :
C++
#define STRING_SIZE  49

int main()
{
  char instr[STRING_SIZE+1];
  char string[STRING_SIZE+1];
  FILE * fr = NULL;
  int flag = 0;
  size_t length = 0;

  fr = fopen("fiile.csv","r");
  if( fr == NULL )
  {
     printf( "Unable to open file\n" );
     return 1;
  }

  printf("Enter your name: ");
  fgets( instr, STRING_SIZE, stdin);
  RemoveNewLines( instr );

  strcat( instr, "," );     // append a comma to the user's entry
  length = strlen( instr );

  while( fgets( string, STRING_SIZE, fr ) )
  {
     RemoveNewLines( string );
     if( strncmp( instr, string, length ) == 0 )
     {
        flag = 1;
        break;      // no need to read any more, unless you want to
     }
  }

  fclose(fr);       // close the file - some OS don't like it if it isn't

  if( flag == 1 )   // I prefer positive logic
  {
     printf( "Your details :" );
     printf( "'%s'\n", string  );
     return 0;
  }

  printf("Access denied.\n");
  return -1;
}
With the comma appended a limited string comparison makes sense assuming the name is followed by a comma in the file.
 
Share this answer
 
Comments
Rick York 20-Aug-20 17:20pm    
NOTE : there is no guarantee this works because I haven't tried it and I can't since I don't have your file. If it doesn't work then this will provide a good opportunity to utilize the debugger.
KarstenK 21-Aug-20 2:41am    
it is better to compare name and password seperatly, to inform the user about the result.
Rick York 21-Aug-20 2:57am    
I agree but there was no mention of a password.
ibilalkayy 21-Aug-20 6:41am    
Hey Rick, I used the 2nd code but the problem which I am facing is, it only accepts the first word which has a comma at the end. The file has many words with commas at the end but it is accepting the first one. For example, if I gave the option to enter your ID card number which is not the first word. It could be the 2nd or 3rd word in the file then how I would handle it?
Rick York 21-Aug-20 11:13am    
To match the other words, you will have to peel apart or separate the words on each line from the file. I usually use strtok to do this and your delimiter would be a comma. It does rather stupid parsing and will catch commas in strings stuff like that so you have to watch for that. This is a separate issue from what you originally asked so if you get stuck you should probably post another question about that part.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900