Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "trabalho.h"
- #include "unistd.h"
- #include "string.h"
- #include "stdlib.h"
- #include <fcntl.h>
- tabela *recursividade(tabela *, tabela *, char *);
- int main(){
- return 0;
- }
- int adicionaFicheiro(tabela *lista) //Recebe como parï¿œmetro a lista que contï¿œm os dados da tabela, retorna 1 com sucesso, 0 com erro
- {
- int resultado;
- FILE *f;
- f = fopen("tabela.txt", "w");
- if (f == NULL) /*printf("Erro ao criar um ficheiro para inserir a tabela\n");*/ resultado = 0;
- else
- {
- while (lista->next != NULL)
- {
- char texto[100];
- snprintf(texto, sizeof(texto), "%d %lf %lf %lf", lista->numero, lista->nota1, lista->nota2, lista->nota3);
- fprintf(f, "%s\n", texto);
- lista = lista->next;
- }
- /*printf("Os dados foram copiados com sucesso para um novo ficheiro chamado 'tabela.txt'\n");*/
- resultado = 1;
- }
- return resultado;
- }
- int adicionaDoFicheiro(char nomeFicheiro[], tabela *lista)
- {
- tabela *node = (tabela*)malloc(sizeof(struct Tabela));
- int fd = open(nomeFicheiro, O_RDONLY);
- char buffer[100000];
- int var = read(fd, buffer, sizeof(buffer));
- int i = 0;
- char *token = strtok(buffer, "\n");
- node = recursividade(node, lista, token);
- while(token != NULL)
- {
- token = strtok(NULL, "\n");
- node = recursividade(node, lista, token);
- }
- }
- tabela *recursividade(tabela *node, tabela *lista, char *token)
- {
- int i = 0;
- node->fullSentence = token;
- char *palavra = strtok(node->fullSentence, " ");
- node->numero = atoi(palavra);
- i++;
- while (token != NULL)
- {
- palavra = strtok(NULL, " ");
- switch (i)
- {
- case 1:
- node->nota1 = atof(palavra);
- i++;
- break;
- case 2:
- node->nota2 = atof(palavra);
- i++;
- break;
- case 3:
- node->nota3 = atof(palavra);
- i++;
- break;
- default:
- break;
- }
- }
- lista->next = node;
- node->next = NULL;
- free(node);
- tabela *nodo = (tabela*)malloc(sizeof(struct Tabela));
- return nodo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement