Search code examples
cfileinputcharfgets

c file input function not working as intended


I have this function that is supposed to check if a username is blacklisted and if it isn't, it's supposed to "log in" the user by checking if that username is present in a second file. If it is, then that line from the file will be remembered and the function should check in a third file if the password on the remembered line is correct, so basically the line of the username is the line of his password and if those two match with the input and the user isn't blacklisted then it should return a postive answer.

Unfortuntely my function seem to be returning a positive answer no matter what I type. Can anyone point out what my mistake is so that it will work as intended ?

Edited out unintentional mistakes, I still have and issue where I can not stop reading if the file isn't in blacklist.txt

code:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>          
#include <arpa/inet.h>
#include <netdb.h>   

int login(char username[20],char pw[20])
{
    FILE *fp = fopen("blacklist.txt","r");          
    int ok = 0, pw_line=0,oku=0,savepwline=0,count=0,okp=0;

    while (ok==0)                                            
    {
        if (fp != NULL)
        {
            char line[20];
            while (fgets(line,sizeof(line), fp) != NULL)
            {                           
                size_t i=strlen(line)-1;
                if(line[i]=='\n')
                line[i]='\0';
                if (strcmp(username,line) == 0 ) ok=1;
            }
        }
    }
    fclose(fp);
    if (ok==1)
    {
        printf("user blacklisted\n");
    }
    else
    {
        FILE *fp2 = fopen("loginuser.txt","r"); 
        while (oku==0)                                            
        {
        if (fp2 != NULL)
        {
            char line3[20];
            while (fgets(line3,sizeof(line3), fp2) != NULL)
            {
                pw_line++;                            
                size_t i=strlen(line3)-1;
                if(line3[i]=='\n')
                line[i]='\0';
                if (strcmp(username,line3) == 0 ) { oku=1; savepwline=pw_line; }
            }
        }
        }
        FILE *fp3 = fopen("loginpw.txt","r"); 
        if (oku==1)
        if (fp3 != NULL)
        {
            char line2[20];
            while (fgets(line2,sizeof(line2), fp3) != NULL)
            {
                if ( count == savepwline )           
                {
                    size_t i=strlen(line2)-1;
                    if(line2[i]=='\n')
                        line2[i]='\0';
                    if (strcmp(pw,line2) == 0 ) okp=1;
                }
                else count++;
            }
        }
    fclose(fp2);
    fclose(fp3);
    }
    if(oku&&okp) return 1;
    else return 0;
}

int main()
{
 char a[20],b[20];
 scanf("%s",a);
 scanf("%s",b);
 if(login(a,b)) printf("yes");
else printf("no");
}

sample file ("loginuser.txt"/"loginpw.txt") :

hihi
aloss
foif
distsd

blacklist.txt:

carl
gigc
ffgfd
gdfgdd
rreti

Solution

  • I can see few problems with the code.

    • It never comes out of while loop if the username is not there in blacklist.txt. Instead read till end of file.
    • Instead of "==", you have given "=" for comparison, which will do an assignment instead of comparison.
    • Check for file pointer validity just after opening file, so that you need not validate it every time in loop. This is a performance hit.
    • Write a function for file read. This will make debugging easier.
    • If you are not able to use a debugger, add few printf statements, that will help you understand the code flow.

    See the corrected code below. I haven't added the password comparison check. You can do it yourself.

    // Returns 1 if found. Returns 0 on not found/error
    int isEntryFound(char *fileName, char *inputStr)
    {
        FILE *fp = fopen(fileName,"r");
        if (fp == NULL)
        {
            // Couldnt verify. Mark as error
            return 0;
        }
        int ok = 0;
    
        char line[20];
        while (fgets(line,sizeof(line), fp) != NULL)
        {
            size_t i=strlen(line)-1;
            if(line[i]=='\n')
                line[i]='\0';
            if (strcmp(inputStr,line) == 0 ) ok=1;
        }
        fclose(fp);
        if(ok == 1)
            return 1;
        return 0;
    
    }
    
    int login(char *username,char pw[20])
    {
        int blackListed = isEntryFound("blacklist.txt", username);
    
        if(blackListed)
        {
            printf("Blacklisted\n");
            return 0;
        }
    
        int userFound = isEntryFound("loginuser.txt", username);
    
        if(!userFound)
        {
            printf("User not Found\n");
            return 0;
        }
    
        // TODO: Read and compare the passwords the same way
        return 0;
    }