How to get Function names?

How read names from a file add one and print them in alphabetical order in C programming?

  • 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. I just need help with a function to print the names in the file in alphabetical order after i add the new employee. This is what I have so far: #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; //-Ask 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); //-Get the number of employees on the roster from the input file fscanf(ifp, "%d", &num_people); for(i = 0; i < num_people; i++){ fscanf(ifp, "%s", temp_employee.lname); fscanf(ifp, "%s", temp_employee.fname); namecmp(temp_employee, new_employee); if (namecmp(temp_employee, new_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); } } //-Set up a loop for the number of employees //-Each time you should do the following: //--Read in a name from the input file //--Use the provided namecmp function to see if the // new employee should be printed first //--Print the old employee to the output file //-If the new employee was never printed, print at the end of the file fclose(ifp); fclose(ofp); return 0; } /* Pre-Conditions: This function takes in two structures of type record. * These are labeled eA and eB for employee A and employee B, respectively. * Post-Conditions: This function compares the names of the two * employee to see which should come first lexicographically. * The function returns -1 if eA comes first, 1 if eB comes first, * and 0 if the employees have the same name. */ 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; } }

  • Answer:

    That's simple. Just click this link: http://answers.yahoo.com/question/index;_ylt=ArUs1.4mU0ENUZGWxHhV_BTsy6IX;_ylv=3?qid=20120420232014AA1wU9M Then read the advice given to you, make the changes, and post your fixed program. If it's clear that you're too lazy to gather advice from somebody and use it then why would people bother giving you advice? You have the same stupid namecmp function, buffer overflows, and numerous errors which aren't handled from calls to fopen and scanf/fscanf. If your program is broken in the first place, then why would you proceed to extend it before fixing the errors? EDIT: If you care about learning programming, then I suggest you try to figure out why copying two entire structs (which roughly 40 chars each) and calling strcmp four times (when it's only necessary to call it two times) are stupid ideas. You are likely to benefit more from that knowledge than getting your program to print names in alphabetical order. EDIT#2: If your professor also taught you to use fopen and forgot to teach you to check the return value, you might consider finding a more knowledgeable professor. The way specification for the program sounds quite strange. It seems like you're meant to do the following: 1. Receive a first and last name from stdin. 2. Open a file which contains a list of first and last names. 3. Read each name from the file and compare it with the name that was read from stdin. If < 0 is returned from namecmp, write the name that was read from stdin along with the current name which was read from the file, otherwise write the current name which was read from the file. If that's all that you're meant to do, then it seems fairly simple to accomplish. Here's what I've come up with: http://ideone.com/pLDDm Usually it would be a better idea write to stdout rather than another, since you can just redirect it to a file later on. Another good idea would be to receive the path to the input file from argv[1], instead of having a fixed path to roster.txt.

Bruno P at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

*Facepalm*-----i had a brain fart from all of this =S

Dylan

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.