1

I have a file that contains information on employees in the following format:

Name: Belal Kassem
Service: Security
Date of Birth: 1995
Salary: 500

Name: Abdallah Yasser
Service: Marketing
Date of Birth: 1954
Salary: 500

Name: Hend Elkarmouty
Service: Dentist
Date of Birth: 1990
Salary: 800

I would like my program to search for an employee's name, which is indicated by the user, and then return true or false (found/not-found).

However, right now, my program only asks for the number of employees I want to remove and their names.

int NumToDelete;
printf("How much employees do you want to remove?\n");
scanf(" %d", &NumToDelete);
fgetc(stdin);
char Name[NumToDelete][25];
for(int i = 0; i < NumToDelete; i++)
{
    printf("Name: ");
    fgets(Name[i], 25, stdin);
    char BarLoc, NameFinder[25];
    int line = 0;
    FILE *fremove = fopen("Employees.txt", "r");
    do
    {

        if((line % 5) == 0)
        {
            fseek(fremove, 6, SEEK_CUR);
            fgets(NameFinder, 25, fremove);
            if(NameFinder == Name[i])
            {                             //This is not the official code.
                printf("%s", NameFinder); //Just to check if it is working or not.
            }                             //Here it is suppose to be the deleting code. 
        }
        BarLoc = getc(fremove);
        if(BarLoc == '\n')
        {
            line++;
        }
    }while(BarLoc != EOF);

I would also like to print out, using the NameFinder variable, all employees which were deleted.

Matthew E. Miller
  • 557
  • 1
  • 5
  • 13
  • 1
    May not be your only problem, but you should read this: https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings – Retired Ninja Aug 18 '19 at 22:15
  • 1
    `if(NameFinder == Name[i])` compares pointers, not string contents. – chux - Reinstate Monica Aug 18 '19 at 22:18
  • If this is a practical problem, I'd recommend putting your data into [SQLite](https://sqlite.org/index.html) and then doing the rest with SQL. – Schwern Aug 18 '19 at 22:18
  • @chux may you suggest a alternative? Should I do something like this: `if(NameFinder == &Name[i])`? – Belal Kassem Aug 18 '19 at 22:25
  • @RetiredNinja yes it worked. my code is now `if(strcmp(NameFinder, Name[i]) == 0)` – Belal Kassem Aug 18 '19 at 22:33
  • You should work toward reading all data from your file in a single pass and separating the employees into either an array of struct, or a linked-list of struct or something similar. That way all employee data is held in memory and you can easily search, add, remove employees as needed and then write the updated data a single time on exit. – David C. Rankin Aug 19 '19 at 00:16

1 Answers1

3

Use strcmp to compare the strings, like this:

if(!strcmp(NameFinder, Name[i]))
{                             //This is not the official code.
   printf("%s", NameFinder); //Just to check if it is working or not.
} 
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
ChauhanTs
  • 439
  • 3
  • 6