Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. PERGUNTAS val;
  2.  
  3. lista_perguntas * aux = original->next;
  4. lista_perguntas * base = original;
  5. lista_perguntas * novo;
  6.  
  7.  
  8. while (aux != NULL) { // eu estava com um problema anteriormente que o primeiro elemento da lista era ignorado por isso eu pesquisei e encontrei esta solução que basicamente ignora esse elemento e passa para o segundo pelo que eu percebi
  9. aux = aux->next;
  10. base = base->next;
  11. }
  12.  
  13.  
  14. while(fread(&val, sizeof(PERGUNTAS), 1, fp)!=-1){
  15.  
  16. novo = (lista_perguntas*) malloc(sizeof (lista_perguntas));
  17. novo->valor = val;
  18. novo->next = aux;
  19. base->next = novo;
  20.  
  21. }
  22.  
  23. #define PERGUNTA_TAM_MAX (256)
  24.  
  25. typedef struct pergunta_s pergunta_t;
  26.  
  27. struct pergunta_s
  28. {
  29. char pergunta[ PERGUNTA_TAM_MAX + 1 ];
  30. pergunta_t * prox;
  31. };
  32.  
  33. int perguntas_carregar( const char * arq, pergunta_t ** perg )
  34. {
  35. FILE * pf = NULL;
  36. pergunta_t * p = NULL;
  37. pergunta_t * pant = NULL;
  38. pergunta_t * pprim = NULL;
  39. int n = 0;
  40. int nread = 0;
  41.  
  42. pf = fopen( arq, "rb" );
  43.  
  44. if(!pf)
  45. return ERRO_LEITURA_ARQUIVO;
  46.  
  47. while(1)
  48. {
  49. p = malloc( sizeof(pergunta_t) );
  50.  
  51. nread = fread( p, sizeof(pergunta_t), 1, pf );
  52.  
  53. if( nread != 1 )
  54. {
  55. free(p);
  56. break;
  57. }
  58.  
  59. p->prox = NULL;
  60.  
  61. if(pant)
  62. pant->prox = p;
  63. else
  64. pprim = p;
  65.  
  66. pant = p;
  67.  
  68. n++;
  69. }
  70.  
  71. fclose(pf);
  72.  
  73. *perg = pprim;
  74.  
  75. return n;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement