I would use the qsort .... bubble sorts are fun and good for smaller datasets.
Anyway here is the array example followed by your request for the output file example too.
9 element array
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( int argc, char *argv[] )
{
int files = argc-1, i = 0, x = 0, flag=0, a=0;
char word[80], list[9][80];
FILE *infile, *outfile;
if ( (outfile = fopen("WordList.txt", "w")) == NULL )
{
fprintf(stderr, "Can't Open Textfile..txt for out put\n");
exit(2);
}
for(i = 1; i <= files; i++)
{
if ( (infile = fopen( argv[i], "r")) == NULL )
{
fprintf(stderr, "Can't Open file %s\n", argv[i]);
continue;
}
else
{
fscanf(infile, "%s", word) ;
while ( !feof(infile) )
{
a=flag=0;
while ( list[a++][0] )
{
if ( strcmp(list[a], word) == 0 )
{
flag=1;
break;
}
}
if ( flag == 0 )
{
strcpy(list[a], word);
printf("%s\n", word);
fprintf(outfile, "%s\n", word);
}
fscanf(infile, "%s", word) ;
}
}
fclose( infile );
}
return 0;
}
File storage example
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( int argc, char *argv[] )
{
int files = argc-1, i = 0, x = 0, flag=0;
char word[80], list[80];
FILE *infile, *outfile, *datafile;
if ( (outfile = fopen("WordList.txt", "w")) == NULL )
{
fprintf(stderr, "Can't Open Textfile..txt for out put\n");
exit(2);
}
if ( (datafile = fopen("WordList.txt", "r")) == NULL )
{
fprintf(stderr, "Can't Open Testfile for read\n");
exit(2);
}
for(i = 1; i <= files; i++)
{
if ( (infile = fopen( argv[i], "r")) == NULL )
{
fprintf(stderr, "Can't Open file %s\n", argv[i]);
continue;
}
else
{
fscanf(infile, "%s", word) ;
while ( !feof(infile) )
{
rewind(datafile);
flag=0;
fscanf(datafile, "%s", list) ;
while ( !feof(datafile) )
{
fscanf(datafile, "%s", list) ;
if ( strcmp(list, word) == 0 )
{
flag=1;
break;
}
}
if ( flag == 0 )
{
printf("%s\n", word);
fprintf(outfile, "%s\n", word);
fflush(outfile);
}
fscanf(infile, "%s", word) ;
}
}
fclose( infile );
}
return 0;
}
hope it helps to see the different ways to handle your problem. btw, I am learning to code this stuff myself too. I don't do this stuff for real either. I just didn't want to be doing some high school kids work.