Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to make a file management system but I need some help with it. I can't seem to figure out how to actually compare strings in C. Here is my current code:

# include <stdlib.h>   // for exit()
# include <stdio.h>    // defines FILE


//project task
//ask for username and password of user.
//make 2 array (1 for username, 1 for password)
int pp;
//global list
  FILE *fptr;    // ptr to file for username
  char ch;
  FILE *x;

  FILE *fptr2;    // ptr to file for password
  char ch2;
  FILE *z;
  char theString[] = "First Mid Last";
  char theString1[100];
  char theString2[100];
  char zzzz[100];
  char storeuser[100];
//global list     
int main()
{
   
     
         
     if ((fptr = fopen("username.txt","r"))== NULL )  // open file in writing mode
      {
        printf("Can't open file username.txt\n");
        getch();
        exit(1);  // error return value
        }       
        
        
        
     if ((fptr2 = fopen("password.txt","r"))== NULL )  // open file in writing mode
      {
        printf("Can't open file password.txt\n");
        getch();
        exit(1);  // error return value
        }       
     printf("Enter your userame\n");
     
   scanf("%s", &theString1);
//   printf("%s", theString1);
     
     printf("Enter your password\n");
     
   scanf("%s", &theString2);
//   printf("%s", theString2);
     
     
     printf("%s", fptr);
     while ((ch = getc(fptr)) != EOF)
     
     
 
     printf("%c",ch);
     
     

     
     fclose(fptr);
     
     getch();
     
     return 0;
        
}


I want to compare theString1 with the text in username.txt which is being accessed using the fptr variable.
Any help regarding this would be appricated :)
Thanks.
Posted

1 solution

Your input file does probably consist of multiple line, each containing a username and password (probably encoded with a hash function). So perform a loop over the lines in your username.txt file. In each loop iteration:

a) extract the username and password out of that line
b) compare the username with the console input in theString1.

You may use the cmpstr or cmpistr (for case-insensitive comparison) to compare the string in theString1 with the username.
 
Share this answer
 
Comments
Nelek 21-Nov-13 13:24pm    
Nice advice. +5
CPallini 21-Nov-13 13:27pm    
5.However there's no thing like case-insensitive comparison (or at least it shouldn't exist) :-)
Ammar_Ahmad 21-Nov-13 15:06pm    
Thanks for the quick reply. Also thanks for the tip regarding the encryption. :)

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