Hello,

In the problem I'm working on now, I'm supposed to take a bunch of files, each containing a string per line, and merge them together so they form one sorted file. Each individual file is already sorted itself. Here is my code so far:

Code:
 #include <stdio.h>
 #include <string.h>

 #define MAX_LINES 10000

 main(int argc, char *argv[]) {
    char buf[BUFSIZ];
    char sorted[MAX_LINES][BUFSIZ] = {0};
    char *tok;
    int stringNum;
    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++;
    }
    
    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);
             else if (strcmp(buf,sorted[j]) == 0) break;
             else if (strcmp(buf,sorted[j]) < 0) {
                for (k = stringNum; k >= j; k--) sorted[k+1] = sorted[k];
                strcpy(sorted[j],buf);
             }
             else if (strcmp(buf,sorted[j]) > 0) continue;
          }
       }
    }
    for (i = 0; i < stringNum; i++) printf("%s", sorted[i]);

 }
What I'm doing here is adding all the lines from the first file into a big array. Then, I want to take each file, compare every string in that file to what's already in the array, and then put it in where it's necessary. The bolded line is what's giving me a problem. There, if the word in buf is smaller than the one in the array, then I want to take every other word in the array and shift it up one spot to make room for the new word. However, when I compile it, I get an "incompatible types in assignment error". I can't figure this out. Also, any tips for making this program better would be appreciated, as I don't think this is the best way of doing it. Also, I'm supposed to compile it as follows: gcc -g -o Merge a4.c
I'm not really clear on what this will do. Will it just let me run the program with the word Merge as opposed to using a.out? For more information on the problem go here: http://www.cs.dal.ca/~sedgwick/2132/a4/problem