I will need to check how many employees are on the old roster. For each employee, I will need to check to see if the NEW employee’s name should be printed before it. If so, print the new employee’s name, followed by the old employee. Otherwise, simply print the old employee’s name. This should repeat until all the names (new and old) have been printed to the new roster.

This is what I have so far:

Code:


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


struct record{
    char fname[20];
    char lname[20];
};


int namecmp (struct record eA, struct record eB);


int main() {
    FILE *ifp = fopen("input.txt", "r");
    FILE *ofp = fopen("roster.txt", "w");
    int num_people, i, flag=0;
    struct record new_employee, temp_employee;



    // Asking the user for the first and last names of the new employee
    printf("What is the first name of the new employee?\n");
    scanf("%s", new_employee.fname);

    printf("What is the last name of the new employee?\n");
    scanf("%s",  new_employee.lname);

    // Geting the number of employees on the roster from the input file
    fscanf(ifp, "%d", &num_people);

    //For loop to scan and print empolyees
    for(i = 0; i < num_people; i++){
        fscanf(ifp, "%s", temp_employee.lname);
        fscanf(ifp, "%s", temp_employee.fname);

        namecmp(new_employee, temp_employee);

        if (namecmp(new_employee, temp_employee) < 0 ) {
            fprintf(ofp, "%s %s\n", new_employee.fname, new_employee.lname);
            fprintf(ofp, "%s %s\n", temp_employee.fname, temp_employee.lname);
        }
        else {
            fprintf(ofp, "%s %s\n", temp_employee.fname, temp_employee.lname);

        }

    }    

    fclose(ifp);
    fclose(ofp);


    return 0;
}




int namecmp (struct record eA, struct record eB) {
    if (strcmp(eA.lname, eB.lname) < 0)
        return -1;
    else if (strcmp(eA.lname, eB.lname) > 0)
        return 1;
    else {
        if (strcmp(eA.fname, eB.fname) < 0)
            return -1;
        else if (strcmp(eA.fname, eB.fname) > 0)
            return 1;
        else
            return 0;
    }
}



I am having trouble with a function to print the names in the file in alphabetical order.