Вы, по сути, пишете основные функции утилиты unix / linux 'uniq'.
cat filename | sort | uniq > newfile
#or skip sort, since you didn't mention
cat filename | uniq > newfile
Вы можете просто использовать popen и uniq (что-то вроде этого ...)
FILE *uniqfh;
uniqfh = popen("cat file1 | uniq" , "r");
if (uniqfh == NULL) { //handle error
}
while( fgets(uniqfh, buffer, buffersize) ) printf("%s\n",buffer);
А если серьезно, вы могли бы написать ядро uniq (),
static long MAXUNIQ=79; //or whatever you want
char*
isdup(char* prev, char* next, long len)
{
//if( !prev || !next) error
long n = len<=0 ? MAXUNIQ : len;
for( ; *prev==*next && n --> 0; ) { //down-to operator (sic)
; //clearly nothing happening here!
}
return( (n<1) || !(*p+*n) );
}
/yeah, this is actually strncmp, but hey
Вам нужен массив 'строк' (char * или char []), давайте прочитаем их,
char* ray[ARRAYMAX]; //define how many elements of your arRay
//could use, char** ray; and malloc(ARRAYMAX*sizeof(char*))
long
read_array(FILE* fh, char* ray[])
{
char buffer[MAXLINE+1];
long count=0;
while( fgets(buffer,sizeof(buffer),fh) ) {
//you could eat dups here, or in separate function below
//if( (count<1) && !isdup(ray[count-1],buffer,MAXUNIQ) )
ray[count++] = strdup(buffer);
}
//ray[0] through ray[count-1] contain char*
//count contains number of strings read
return count;
}
long
deleteIdents(long raysize, char* ray[]) //de-duplicate
{
long kept, ndx;
for( ndx=1, kept=0; ndx<raysize; ++ndx ) {
if( !isdup(ray[kept],ray[ndx]) ) {
ray[kept++] = ray[ndx];
}
else {
free(ray[ndx]);
ray[ndx] = NULL; //not entirely necessary,
}
}
return kept; //new ray size
}
И вам нужно будет это назвать ...
...
long raysize;
char* ray[ARRAYMAX] = {0}; //init to null pointers
raysize = read_array(fopen(filename,"r"),ray);
raysize = deleteIndents(raysize,ray);
...
Позже вам нужно будет освободить строки malloc,
for( ; 0 <-- raysize; ) { free(ray[raysize]); ray[raysize] = NULL; }