- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare_str(const void* a, const void* b)
{
return strcmp((char*)a, (char*)b);
}
int main(int argc, char** argv)
{
int len = 32, total = 0, j = 0;
char* pbase = new char[len * 10000];
char* p = pbase;
FILE* fp = fopen("test.txt", "r");
while (fgets(p, len, fp))
{
p[strlen(p) - 1] = 0;
total++;
p += len;
}
fclose(fp);
qsort(pbase, total, len, compare_str);
char buf[256];
strcpy(buf, pbase);
p = pbase;
for (total; total > 0; total--, p += len)
{
if (strcmp(buf, p) == 0)
j++;
else
{
printf("%d %s\n", j, buf);
strcpy(buf, p);
j = 1;
}
}
return 0;
} |
|