separe as funções do código em bibliotecas: #include <stdio.h> #include <stdlib.h>…
- User
separe as funções do código em bibliotecas:
#include <stdio.h> #include <stdlib.h> #include <string.h>
void le_string(char string[], int tamanho) { setbuf(stdin, NULL); fgets(string, tamanho - 1, stdin); string[strcspn(string, "\n")] = '\0'; }
typedef struct { int codigo; char titulo[30]; char genero[30]; int classificacao; char plataforma[30]; int quantidade_de_temporada; int duracao_media; int *quantidade_de_ep; } Serie;
Serie *retorna_preenchido(int *i, Serie *serie) { // abre o arquivo. FILE *arquivo = fopen("streaming_db.csv", "r"); if (arquivo == NULL) { perror("\nErro ao abrir o arquivo!\n"); exit(1); }
// Declarando as variaveis. char *pt; char vetor_guarda_linha[150]; fgets(vetor_guarda_linha, 150, arquivo); serie = (Serie *)malloc(sizeof(Serie)); while (fgets(vetor_guarda_linha, 150, arquivo) != NULL) { serie = (Serie *)realloc(serie, (*(i) + 1) * sizeof(Serie)); pt = strtok(vetor_guarda_linha, ","); serie[*(i)].codigo = atoi(pt); pt = strtok(NULL, ","); strcpy(serie[*(i)].titulo, pt); pt = strtok(NULL, ","); strcpy(serie[*(i)].genero, pt); pt = strtok(NULL, ","); serie[*(i)].classificacao = atoi(pt); pt = strtok(NULL, ","); strcpy(serie[*(i)].plataforma, pt); pt = strtok(NULL, ","); serie[*(i)].duracao_media = atoi(pt); pt = strtok(NULL, ","); serie[*(i)].quantidade_de_temporada = atoi(pt); serie[*(i)].quantidade_de_ep = (int *)malloc(serie[*(i)].quantidade_de_temporada * sizeof(int)); for (int j = 0; j < serie[*(i)].quantidade_de_temporada; j++) { pt = strtok(NULL, ", "); serie[*(i)].quantidade_de_ep[j] = atoi(pt); } (*i)++; } fclose(arquivo); return serie;}
Serie *inserir(int *qtd_de_inserir, Serie *vetor, int *aux, int *auxI) { i
- Assistant
n this case, you can separate the code into three separate libraries - one for input processing, one for handling the structure of the series, and one for file management. The libraries would look like this:
input_processing.h:
#include <stdio.h> #include <string.h> void le_string(char string[], int tamanho);input_processing.c:
#include "input_processing.h" void le_string(char string[], int tamanho) { setbuf(stdin, NULL); fgets(string, tamanho - 1, stdin); string[strcspn(string, "\n")] = '\0'; }serie_structure.h:
#include <stdlib.h> typedef struct { int codigo; char titulo[30]; char genero[30]; int classificacao; char plataforma[30]; int quantidade_de_temporada; int duracao_media; int *quantidade_de_ep; } Serie; Serie *retorna_preenchido(int *i, Serie *serie); Serie *inserir(int *qtd_de_inserir, Serie *vetor, int *aux, int *auxI);serie_structure.c:
#include "serie_structure.h" #include "file_management.h" Serie *retorna_preenchido(int *i, Serie *serie) { // Function body from original code } Serie *inserir(int *qtd_de_inserir, Serie *vetor, int *aux, int *auxI) { // Function body from original code }file_management.h:
#include <stdio.h> FILE *open_file(const char *file_name, const char *mode); void close_file(FILE *file);file_management.c:
#include "file_management.h" FILE *open_file(const char *file_name, const char *mode) { FILE *file = fopen(file_name, mode); if (file == NULL) { perror("\nErro ao abrir o arquivo!\n"); exit(1); } return file; } void close_file(FILE *file) { fclose(file); }Now, to use these libraries in your main code, you just need to include them at the beginning of your file:
#include "input_processing.h" #include "serie_structure.h" #include "file_management.h" // The rest of your main code