Thread: How to read names from a file, add one and then sort them in alphabetical order

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    3

    How to read names from a file, add one and then sort them in alphabetical order

    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.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Your namecmp function looks fine to me. strcmp is case sensitive (lowercase > uppercase), so that might cause odd results if you capitalised the names in the file but couldn't be bothered when typing them it You could use case insensitive strcasecmp() if it's available, if not then write your own or standardise the case first.

    Code:
        for(i = 0; i < num_people; i++){
            fscanf(ifp, "%s", temp_employee.lname);
            fscanf(ifp, "%s", temp_employee.fname);
    Seems like everywhere else you're doing it FirstName LastName, but reading from the file you're doing LastName Firstname. Intentional?

    Finally,
    Code:
    if (namecmp(new_employee, temp_employee) < 0 ) {
    This will be true when you need to print the name first, but if your input file is in alphabetical order it'll continue to be true for every name, so you'll get lots of duplicates of the new employee.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    yes this is what my input file looks like:

    3
    Abdul Rachel
    Lopez Charlie
    Schofield Theo

    Its by last name, first name. When i print the file it has to be first, last.

    and yes that is my problem when ever i try to print the file it just gives me duplicates of the new employee.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    To sort them you will want to load them into memory first.

    For that you should have an array, or linked-list. At the moment you're putting them into the same place in memory, overwriting the previous name it held each time.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arranging a file into alphabetical order.
    By Malachi in forum C Programming
    Replies: 18
    Last Post: 02-10-2009, 11:07 PM
  2. names in alphabetical order?
    By n3cr0_l0rd in forum C Programming
    Replies: 21
    Last Post: 02-06-2009, 08:59 PM
  3. String sort in alphabetical order
    By ThLstN in forum C Programming
    Replies: 6
    Last Post: 01-06-2008, 02:59 AM
  4. Help me sort the names in alpha order.
    By cazil in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2002, 02:30 PM
  5. Alphabetical order
    By BubbleMan in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 03:38 PM