Code:
/*
Description: This program reads two files, stores each in its own array, sorts them and merge both sorted arrays;
then, writes the content in an output file.
The program then extracts names of people that live in 2 cities and write them in another output file.
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <assert.h>
void readFile(char** store, char* file);
void sort(char **array1, int count);
char ** merge(char **store1, char ** store2, int count1, int count2);//merge page 508
void extract(char **store);
void writeFile(char **store, char *file);
int main()
{
char * array1[200];
char * array2[200];
char buffer[100];
int i;
int numLine = 0;
int numLine2 = 0;
FILE * handle2 = fopen("FILE2.TXT", "r");
FILE * handle = fopen("FILE1.TXT", "r");// r is for read
if( handle == NULL )//if does not exist will not open
{
printf( "Cannot open input file" );
return -1;
}
if( handle2 == NULL ) {
printf( "Cannot open input file" );
return -1;
}
while (fgets(buffer,100, handle) != NULL)
{
if (strchr(buffer, '\n') != NULL)
*strchr(buffer, '\n') = '\0'; /* get rid of the '\n' */
array1[numLine] = (char *) malloc(strlen(buffer) + 1);
assert(array1[numLine] != NULL);//if condition is true continue
strcpy(array1[numLine++], buffer);//copy content of buffer into array1
}
fprintf(stdout, "These are the %d strings that were read from the input file\n", numLine);
/*
for (i = 0; i < numLine; i++) {
printf("%s\n",array1[i]);
}
*/
while (fgets(buffer,100, handle2) != NULL)
{
if (strchr(buffer, '\n') != NULL)
*strchr(buffer, '\n') = '\0'; /* get rid of the '\n' */
array2[numLine2] = (char *) malloc(strlen(buffer) + 1);
assert(array2[numLine2] != NULL);
strcpy(array2[numLine2++], buffer);
}
fprintf(stdout, "These are the %d strings that were read from the input file\n", numLine2);
/*
for (i = 0; i < numLine2; i++)
{
printf("%s\n",array2[i]);
}
*/
sort(array1, numLine);
sort(array2, numLine2);
merge(array1,array2, numLine,numLine2);
//fillArray(personArray_1, fpin_1, &count1);
//fillArray(personArray_2, fpin_2, &count2);
//sort(personArray_1, count1);
//sort(personArray_2, count2);
//merge(personArray_1, count1, personArray_2, count2, mergedArray);
//writeToOutputFile(mergedArray, count1+count2, fpout_1);
//filterArray(mergedArray, count1+count2, filteredArray, &filteredCount, "Leominster");
//writeToOutputFile(filteredArray, filteredCount, fpout_2);
//filteredCount = 0;
//filterArray(mergedArray, count1+count2, filteredArray, &filteredCount, "Worcester");
// writeToOutputFile(filteredArray, filteredCount, fpout_2);
/*fclose(fpin_1);
fclose(fpin_2);
fclose(fpout_1);
fclose(fpout_2);
*/
return 0;
printf("The sorted array has these: \n");
for(i = 0; i < numLine; i++)
printf("%s", array1[i]);
fclose(handle);
fclose(handle2);
return 0;
}
//function to sort - iterate over the array
void readFile(char** store, char* file)
{
}
void sort(char **words, int count) {
int ii, jj, min_index;
char *temp;
for (ii = 0; ii < count; ii++)
{
printf("%s\n",words[ii]);
}
for (ii = 0; ii < count; ii++) {
min_index = ii;
for (jj = ii + 1; jj < count; jj++) {
if (strcmp(words[jj], words[min_index]) < 0 )//if jj < 2nd element switch
min_index = jj;
}
/* now swap the pointers at locations ii and min_index */
temp = words[min_index];
words[min_index] = words[ii];
words[ii] = temp;
}
}
char ** merge(char **store1, char **store2, int count1, int count2)//merge page 508
{
char ** temp = malloc(sizeof(char * ) * (count1 + count2));
int i = 0;
int j = 0;
int k = 0;
int shortLength = count1 > count2 ? count2:count1;// looks at condition (count1>count2) - if true, shortlenght get value of count2 - if not, then get count1
while(j < shortLength)
{
if(strcmp(store1[i],store2[k]) < 0)//comparing one element of 1 array to the element of the other array
{
temp[j] = malloc(strlen(store2[k]) + 1);
strcpy(temp[j],store2[k]);
k++;
}
else{
temp[j] = malloc(strlen(store1[i]) + 1);
strcpy(temp[j],store1[i]);
i++;
}
j++;
}
if(count1 > count2)
{
while(i < count1) {
temp[j] = malloc(strlen(store1[i]) + 1);
strcpy(temp[j],store1[i]);
i++;
j++;
}
}
if(count1 < count2)
{
while(k < count2){
temp[j] = malloc(strlen(store2[k]) + 1);
strcpy(temp[j], store2[k]);
k++;
j++;}
}
return temp;
}
void extract(char **store)
{
}
void writeFile(char **store, char * file)
{
}
"
Here is my input data