Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.2KB

  1. #include "uthash.h"
  2. #include <stdlib.h> /* malloc */
  3. #include <errno.h> /* perror */
  4. #include <stdio.h> /* printf */
  5. #define BUFLEN 20
  6. #if 0
  7. /* Print a message if the hash's no-expand flag is set. */
  8. #undef uthash_noexpand_fyi
  9. #undef uthash_expand_fyi
  10. #define uthash_noexpand_fyi(tbl) printf("noexpand set\n");
  11. #define uthash_expand_fyi(tbl) printf("hash expanded\n");
  12. #endif
  13. typedef struct name_rec {
  14. char boy_name[BUFLEN];
  15. UT_hash_handle hh;
  16. } name_rec;
  17. static int namecmp(void *_a, void *_b)
  18. {
  19. name_rec *a = (name_rec*)_a;
  20. name_rec *b = (name_rec*)_b;
  21. return strcmp(a->boy_name,b->boy_name);
  22. }
  23. int main()
  24. {
  25. name_rec *name, *names=NULL;
  26. char linebuf[BUFLEN];
  27. FILE *file;
  28. file = fopen( "test11.dat", "r" );
  29. if (file == NULL) {
  30. perror("can't open: ");
  31. exit(-1);
  32. }
  33. while (fgets(linebuf,BUFLEN,file) != NULL) {
  34. name = (name_rec*)malloc(sizeof(name_rec));
  35. if (name == NULL) {
  36. exit(-1);
  37. }
  38. strcpy(name->boy_name, linebuf);
  39. HASH_ADD_STR(names,boy_name,name);
  40. }
  41. fclose(file);
  42. HASH_SORT(names,namecmp);
  43. for(name=names; name!=NULL; name=(name_rec*)(name->hh.next)) {
  44. printf("%s",name->boy_name);
  45. }
  46. return 0;
  47. }