Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. int dump_med(const char *fname, struct medico **headpp)
  2. {
  3.     struct medico tmp;
  4.    
  5.     FILE *f = fopen(fname, "r");
  6.     if (f == NULL)
  7.     {
  8.         perror(fname);
  9.         return -1;
  10.     }
  11.  
  12.     while (fscanf(f, "%[^\n]", tmp.nome) == 1 &&
  13.            fscanf(f, "%s %d.%d--%d.%d\n",
  14.                   tmp.especialidade, &(tmp.entrada.h), &(tmp.entrada.m),
  15.                   &(tmp.saida.h), &(tmp.saida.m)) == 5)
  16.     {
  17.         if (!(*headpp = malloc(sizeof **headpp)))
  18.         {
  19.             perror("Failed to allocate new list node: ");
  20.             return -1;
  21.         }
  22.        
  23.         // save, have headpp address the next pointer of the new node
  24.         **headpp = tmp;
  25.         headpp = &(*headpp)->next;
  26.     }
  27.     *headpp = NULL; // terminated
  28.    
  29.     fclose(f);
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement