Okay well I finished the program, but I have one question. What is the second arguement? Why don't you make that the data filepath as in:
executablename datafilepath 1
Because this would make the most sense, but for now here is the code without setting it up like that, but arg 2 is doesn't mean crap all for now, unless you want that to mean something. Let me know.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct empDetails {
int empNumber;
char empLastName[20];
char empFirstName[20];
float empRate;
struct empDetails *next;
};
struct empDetails * BuildList(struct empDetails *);
void PrintList(struct empDetails *);
struct empDetails * SortList(struct empDetails *, int);
int SortField(int argc , char *argv[] );
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
struct empDetails * list = NULL;
int field = SortField(argc,argv);
list = BuildList(list);
PrintList(list);
printf("Above is the unsorted list.\n\n");
list = SortList(list, field);
PrintList(list);
printf("Above is the sorted list.\n\n");
return 0;
}
////////////////////////////////////////////////////////
struct empDetails * BuildList(struct empDetails * hp)
{
FILE *records;
struct empDetails *newp = NULL;
if ((records = fopen("recordFile.txt", "r")) == NULL) {
perror("\nError - No data file!");
exit(1);
}
newp = (struct empDetails *) malloc(sizeof(struct empDetails));
while( fscanf(records, "%d%s%s%f", &newp->empNumber, newp->empLastName,
newp->empFirstName, &newp->empRate) != EOF)
{
newp->next = NULL;
if(hp == NULL)
{
hp = newp;
}else
{
newp->next = hp;
hp = newp;
}
newp = (struct empDetails *) malloc(sizeof(struct empDetails));
}
//delete allocated memory for the last node which is not needed
free(newp);
fclose(records);
return hp;
}
////////////////////////////////////////////////////////////////////////
void PrintList(struct empDetails * hp)
{
while(hp)
{
printf("%d %s %s %.2f is at memory address %p\n", hp->empNumber,
hp->empLastName, hp->empFirstName, hp->empRate, hp);
hp = hp->next;
}
}
//////////////////////////////////////////////////////////////////
struct empDetails * SortList(struct empDetails * hp, int field)
{
struct empDetails *freep = NULL, *returnp = NULL;
struct empDetails *prep = NULL, *curtp = NULL;
struct empDetails *newp = NULL;
while(hp)
{
//node for new list that will be returned
newp = (struct empDetails *) malloc (sizeof(struct empDetails));
newp->empNumber = hp->empNumber;
strcpy(newp->empLastName, hp->empLastName);
strcpy(newp->empFirstName, hp->empFirstName);
newp->empRate = hp->empRate;
newp->next = NULL;
//get rid of old list node by node
freep = hp;
//If this is the first node of the new list
if(returnp == NULL)
{
returnp = newp;
}
if(field == 1 && newp->empNumber > returnp->empNumber)
{
newp->next = returnp;
returnp = newp;
}
if(field == 2 && strcmp(newp->empLastName,returnp->empLastName) > 0)
{
newp->next = returnp;
returnp = newp;
}
if( field == 3 && strcmp(newp->empFirstName, returnp->empFirstName) > 0)
{
newp->next = returnp;
returnp = newp;
}
if(field == 4 && newp->empRate > returnp->empRate)
{
newp->next = returnp;
returnp = newp;
}
else
{
prep = returnp;
curtp = returnp->next;
if(field == 1) while(curtp != NULL && newp->empNumber < curtp->empNumber)
if(field == 2) while(curtp != NULL && strcmp(newp->empLastName,curtp->empLastName) < 0)
if(field == 3) while(curtp != NULL && strcmp(newp->empFirstName,curtp->empFirstName) < 0)
if(field == 4) while(curtp != NULL && newp->empRate < curtp->empRate)
{
prep = curtp;
curtp = curtp->next;
}
prep->next = newp;
newp->next = curtp;
}//end of nested if else statments
//get next node of old list
hp = hp->next;
//free used node of old list
free(freep);
}//end of while
return returnp;
}
////////////////////////////////////////////////////////
int SortField(int argc , char *argv[] )
{
/*
sorting based on 3rd token from the command line
if 1 then sort based on empNumber
if 2 then sort based on empLastName
if 3 then sort based on empFirstName
if 4 then sort based on empRate
*/
int field;
if (argc != 3) {
printf("\nError - Incorrect number of arguements.");
printf("\nSyntax: %s filename field.\n", argv[0]);
exit(1);
}
field = atoi(argv[2]);
if ((field < 1) || (field > 4)) {
printf("\nError - Field must be an integer value in range 1 to 4\n");
exit(1);
}
return field;
}