#include #include #define N_INPUTS 3072 // 1024 pixel * 3 (R, G, B) // Siccome il char è un byte che rappresenta il valore tra 0 e 255. Per evitare confusioni definisco il tipo "byte" come in Java typedef unsigned char byte; // Singola istanza del dataset. typedef struct { byte classificazione; byte dati[N_INPUTS]; } Istanza; // Questo tipo fornisce il vettore delle istanze e il size (dimensione) del vettore typedef struct { int size; Istanza *istanze; } Dataset; Dataset *get_dataset(char *); // Questo metodo legge il file in questione e restituisce un puntatore a Dataset se il file esiste, altrimenti NULL // Ritorna un puntatore perchè in questo caso posso gestire il ritorno NULL. Dataset *get_dataset(char *path) { Dataset *set = (Dataset *)malloc(sizeof(Dataset)); FILE *file; Istanza istanza; Istanza *istanze = (Istanza *)malloc(sizeof(Istanza)); file = fopen(path, "rb"); if (file == NULL) return NULL; int numero_righe = 0; // Fino a quando questo fread restituisce 1 significa che il file contiene ancora roba while (fread(&istanze[numero_righe].classificazione, sizeof(byte), 1, file) == 1) { if (fread(istanze[numero_righe].dati, sizeof(byte), N_INPUTS, file) == N_INPUTS) { numero_righe++; istanze = (Istanza *)realloc(istanze, sizeof(Istanza) * (numero_righe + 1)); // printf("Caricata nel sistema riga %d\n", numero_righe); } } // Dataset set; (*set).size = numero_righe; (*set).istanze = istanze; fclose(file); return set; } /* void salva_dataset(const char *filename, Dataset *set) { FILE *file = fopen(filename, "wb"); if (!file) { perror("Errore nell'apertura del file"); exit(EXIT_FAILURE); } for (int indice_istanze = 0; indice_istanze < set->size; indice_istanze++) { fwrite(&set->istanze[indice_istanze].classificazione, sizeof(byte), 1, file); fwrite(&set->istanze[indice_istanze].dati, sizeof(byte), N_INPUTS, file); } fclose(file); } */