I have a problem with registering a new employee for my project.
It can only detect the first employeeID as an existing..
Here's my code:

void registerNewEmployee(){
   FILE *fp;
   struct employee obj;
   fp=fopen("data.txt","a+");
   system("cls");
   printf("\n\t*******************************************");
   printf("\n\t*******************************************");
   printf("\n\t***\t\t\t\t\t***");
   printf("\n\t***\tNEW EMPLOYEE REGISTRATION\t***");
   printf("\n\t***\t\t\t\t\t***");
   printf("\n\t*******************************************");
   printf("\n\t*******************************************\n\n");
   char employeeIDNew[5];
   printf("Employee Number: ");
   scanf("%s", &employeeIDNew);
   while(fscanf(fp,"%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW) == 4){
      if(strcmpi(obj.employeeID,employeeIDNew)==0){
         printf("Already Exits!");
      }
      else{
         fscanf(fp,"%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW);
         printf("First Name: ");
         scanf(" %s",obj.employeeFN);
         printf("Last Name: ");
         scanf("%s",&obj.employeeLN);
         printf("Password: ");
         scanf("%s",&obj.employeePW);
         strcpy(obj.employeeID,employeeIDNew);
         fprintf(fp,"\n%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW);
         break;
      }
   }
   fclose(fp);
}

Example output that I am getting:

Employee Number: 0001
Already Exist!

-----------------------------

Employee Number: 0002
First Name: test
Last Name: employee
Password: testpw

----------------------------

The 0002 entry is not suppose to ask for employee details.
Also even it did ask for the details, it did not add in data.txt

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

Shouldn't this

scanf("%s", &employeeIDNew);

be

scanf("%s", employeeIDNew);

I already tried the

scanf("%s", employeeIDNew);

still the same output.

You're opening your file in "a+" mode. This doesn't mean there's a read position and a write position. The file position is initially at the beginning of the file for reading, but the instant you do a write, the position is set to the end of the file. Subsequent reads without an intervening seek or rewind will still happen at the end, which accomplishes nothing after your first record is added.

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.