I have attached data.txt file with contains all of the employees data.
The problem is when I login, it would only read one record.
Here's my code:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

int login();
void displayWelcome();


main(){
   int result;
   do{
      system("cls");
      result = login();
      if(result==1 || result==2){
         system("cls");
         if(result==1){
            displayWelcome();

         }
         else{
            printf("Welcome Admin!");
         }
      } 
      else {
         printf("Wrong ID/Password. Please try again\n"); 
         getch();  
      }
   }while(result!=1 && result!=2);

   system("pause");   
}

struct employee{
    char employeeID [5];
    char employeeFN [20],employeeLN [20], employeePW [20];
};

int login(){
   FILE *fp;
   struct employee obj;
   fp=fopen("data.txt","r");
   printf("EmployeeID:");
   char loginEmployeeID[5];
   scanf("%s", &loginEmployeeID);
   printf("Password:");
   char loginEmployeePW[20];
   scanf("%s", &loginEmployeePW);
   while(!feof(fp)){
      fscanf(fp,"%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW);
      if(strcmpi(obj.employeeID,loginEmployeeID)==0 && strcmpi(obj.employeePW,loginEmployeePW)==0){
         return 1;
      }
      else if(strcmpi("admin",loginEmployeeID)==0 && strcmpi("admin",loginEmployeePW)==0){

         return 2;
      }
      else {
         return 0;
      }
   } 
   fclose(fp);
}

void displayWelcome(){
   FILE *fp;
   struct employee obj;
   fp=fopen("data.txt","r");
   fscanf(fp,"%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW);
   printf("Welcome %s %s!\n", obj.employeeFN, obj.employeeLN);
   fclose(fp);
}

This code would produce the ff:
Employee ID:admin
Password:admin

Welcome Admin!

--------------------------------------------
Employee ID:0001
Password:password

Welcome Test Employee!

Employee ID: 0002
Password: 2employ

Wrong ID/Password. Please try again (this should not be wrong.)

If anybody could help me with this, it would be greatly appreciated.

The problem is when I login, it would only read one record.

That's exactly what you tell it to do. Technically, your file reading loop in login is pointless because the first iteration returns from the function in all cases.

I suspect you intended to return 0 only if the loop exits normally:

while(fscanf(fp,"%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW) == 4){
  if(strcmpi(obj.employeeID,loginEmployeeID)==0 && strcmpi(obj.employeePW,loginEmployeePW)==0){
     return 1;
  }
  else if(strcmpi("admin",loginEmployeeID)==0 && strcmpi("admin",loginEmployeePW)==0){
     return 2;
  }
}

fclose(fp);

return 0;

Take a look at the loop condition as well; I fixed a bug for you. feof should not be used to control the loop. Due to timing of events, it introduces an off-by-one error where the last line may be processed twice.

commented: It works perfectly..thank you so much! +0

Because you are displaying the record of the employee, who is logging in by comparing his id and name.

But what output you want???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.