Question
the program prints the list of names sorted by last name but what can i add to print names sortd by first name (first name
the program prints the list of names sorted by last name
but what can i add to print names sortd by first name (first name last name)
any help is much more appreciated
#include #include #include
#define MAX_LEN 100 // Length of each line in input file.
int main(void) { char *strFileName = "C:\\Users\\INTEL\\Desktop\\projects\\abc.txt"; char *strFileSummary = "C:\\Users\\INTEL\\Desktop\\projects\\abc1.txt"; char strTempData[MAX_LEN]; char **strData = NULL; // String List int i, j; int noOfLines = 0;
FILE * ptrFileLog = NULL; FILE * ptrSummary = NULL;
if ( (ptrFileLog = fopen(strFileName, "r")) == NULL ) { fprintf(stderr,"Error: Could not open %s ",strFileName); return 1; } if ( (ptrSummary = fopen(strFileSummary, "a")) == NULL ) { fprintf(stderr,"Error: Could not open %s ",strFileSummary); return 1; }
// Read and store in a string list. while(fgets(strTempData, MAX_LEN, ptrFileLog) != NULL) { // Remove the trailing newline character if(strchr(strTempData,' ')) strTempData[strlen(strTempData)-1] = '\0'; strData = (char**)realloc(strData, sizeof(char**)*(noOfLines+1)); strData[noOfLines] = (char*)calloc(MAX_LEN,sizeof(char)); strcpy(strData[noOfLines], strTempData); noOfLines++; } // Sort the array. for(i= 0; i < (noOfLines - 1); ++i) { for(j = 0; j < ( noOfLines - i - 1); ++j) { if(strcmp(strData[j], strData[j+1]) > 0) { strcpy(strTempData, strData[j]); strcpy(strData[j], strData[j+1]); strcpy(strData[j+1], strTempData); } } } // Write it to outfile. file. for(i = 0; i < noOfLines; i++) fprintf(ptrSummary,"%s ",strData[i]); // free each string for(i = 0; i < noOfLines; i++) free(strData[i]); // free string list. free(strData); fclose(ptrFileLog); fclose(ptrSummary); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started