Ok, here's what I have now.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINES 1000
main(int argc, char *argv[]) {
char buf[BUFSIZ];
char sorted[MAX_LINES][BUFSIZ] = {0};
int stringNum = 0;
int i;
int j;
int k;
FILE *fp;
if (argc > 10) {
fprintf(stderr, "%s: Too many command line arguments!\n",argv[0]);
exit(1);
}
fp = fopen(argv[1],"r");
while ( fgets(buf, BUFSIZ, fp) ) {
strcpy(sorted[stringNum],buf);
stringNum++;
}
if (argc > 2) {
for (i = 2; i < argc; i++) {
fp = fopen(argv[i],"r");
while ( fgets(buf, BUFSIZ, fp) ){
for (j = 0; j < stringNum; j++) {
if (sorted[j] == NULL) {
strcpy(sorted[j],buf);
stringNum++;
}
else if (strcmp(buf,sorted[j]) == 0) break;
else if (strcmp(buf,sorted[j]) < 0) {
for (k = stringNum; k >= j; k--) strcpy(sorted[k+1],sorted[k]);
strcpy(sorted[j],buf);
stringNum++;
}
else if (strcmp(buf,sorted[j]) > 0) continue;
}
}
}
}
for (i = 0; i < stringNum; i++) printf("%s", sorted[i]);
}
This compiles with no errors and works for one file. However, when I try to merge more than one file, it produces a Segmentation Fault (I really hate those things). Any suggestions for what might be going wrong? I have a feeling it has to do with the moving of strings in the sorted array when buf is smaller than the element in sorted.