You need a string-dictionary for this case, like map<string,int> in C++/STL.
A simple example see below. (works only for 32-bit int)
If you need more performance you can
- replace malloc/free with a memory-manager (search in www)
- replace bsearch/qsort with a search-tree (RB,AVL,...)
Code:
#define MAXSTRING 256
typedef struct {
char **a;
int i;
} StrDict;
int cmp(const void*a,const void *b) {
return strcmp(*(char**)a+4,*(char**)b+4); }
void add(StrDict *a,char *s) {
char tmp[MAXSTRING],*x=tmp;
int **p=bsearch((strcpy(x+4,s),&x),a->a,a->i,sizeof*a->a,cmp);
if( p )
(**p)++;
else
strcpy((a->a[++a->i-1]=calloc(1,5+strlen(s)))+4,s),qsort(a->a,a->i,sizeof*a->a,cmp);
}
int main()
{
StrDict sd={malloc(1000000*sizeof*sd.a)}; /* max 1 mio entries */
long last = 0;
char line[MAXSTRING],z[MAXSTRING];
FILE *f = fopen("test.txt","r");
assert( sizeof(int)==4 ); /* !!! */
while( fgets(line,MAXSTRING,f) ) {
if( atol(line)-last>5 && 1==sscanf(line,"%*s%s",z) )
add(&sd,z);
last=atol(line);
}
fclose(f);
while( sd.i-- )
printf("%5d %s\n",*(int*)sd.a[sd.i]+1,sd.a[sd.i]+4),free(sd.a[sd.i]);
free(sd.a);
return 0;
}