Advertisement
Guest User

PL_Parte_C

a guest
Jan 20th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "trabalho.h"
  3. #include "unistd.h"
  4. #include "string.h"
  5. #include "stdlib.h"
  6. #include <fcntl.h>
  7.  
  8. tabela *recursividade(tabela *, tabela *, char *);
  9.  
  10. int main(){
  11.  
  12.  
  13. return 0;
  14. }
  15.  
  16. int adicionaFicheiro(tabela *lista) //Recebe como parï¿œmetro a lista que contï¿œm os dados da tabela, retorna 1 com sucesso, 0 com erro
  17. {
  18.  
  19. int resultado;
  20.  
  21. FILE *f;
  22. f = fopen("tabela.txt", "w");
  23. if (f == NULL) /*printf("Erro ao criar um ficheiro para inserir a tabela\n");*/ resultado = 0;
  24.  
  25. else
  26. {
  27.  
  28. while (lista->next != NULL)
  29. {
  30.  
  31. char texto[100];
  32. snprintf(texto, sizeof(texto), "%d %lf %lf %lf", lista->numero, lista->nota1, lista->nota2, lista->nota3);
  33. fprintf(f, "%s\n", texto);
  34.  
  35. lista = lista->next;
  36.  
  37. }
  38.  
  39. /*printf("Os dados foram copiados com sucesso para um novo ficheiro chamado 'tabela.txt'\n");*/
  40. resultado = 1;
  41.  
  42. }
  43.  
  44. return resultado;
  45.  
  46. }
  47.  
  48. int adicionaDoFicheiro(char nomeFicheiro[], tabela *lista)
  49. {
  50.  
  51. tabela *node = (tabela*)malloc(sizeof(struct Tabela));
  52.  
  53. int fd = open(nomeFicheiro, O_RDONLY);
  54.  
  55. char buffer[100000];
  56.  
  57. int var = read(fd, buffer, sizeof(buffer));
  58.  
  59. int i = 0;
  60.  
  61. char *token = strtok(buffer, "\n");
  62.  
  63. node = recursividade(node, lista, token);
  64.  
  65. while(token != NULL)
  66. {
  67.  
  68. token = strtok(NULL, "\n");
  69. node = recursividade(node, lista, token);
  70.  
  71. }
  72.  
  73.  
  74. }
  75.  
  76. tabela *recursividade(tabela *node, tabela *lista, char *token)
  77. {
  78.  
  79. int i = 0;
  80.  
  81. node->fullSentence = token;
  82.  
  83. char *palavra = strtok(node->fullSentence, " ");
  84. node->numero = atoi(palavra);
  85. i++;
  86.  
  87. while (token != NULL)
  88. {
  89.  
  90. palavra = strtok(NULL, " ");
  91.  
  92. switch (i)
  93. {
  94.  
  95. case 1:
  96. node->nota1 = atof(palavra);
  97. i++;
  98. break;
  99.  
  100. case 2:
  101. node->nota2 = atof(palavra);
  102. i++;
  103. break;
  104.  
  105. case 3:
  106. node->nota3 = atof(palavra);
  107. i++;
  108. break;
  109.  
  110. default:
  111. break;
  112.  
  113. }
  114.  
  115. }
  116.  
  117. lista->next = node;
  118. node->next = NULL;
  119.  
  120. free(node);
  121. tabela *nodo = (tabela*)malloc(sizeof(struct Tabela));
  122.  
  123. return nodo;
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement