dimanche 18 août 2019

How to make C look into a file and find a certain string?

I have a file that contains information on employees in the same format as follows:

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

And I want my program to look for a certain name indicated by the user and then tell me it found it however when I run my program all it does is ask for the number of employees I wan't to remove and there names(as it's main function is to delete those employees information but to do that i need to figure this out first).

    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);

At the end I would've liked the code to print out the name of the employees who the user chose to delete but from the NameFinder variable.

Aucun commentaire:

Enregistrer un commentaire