#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ int…
- User
#include <stdio.h> #include <stdlib.h> #include <string.h>
typedef struct{
codeint codice_cane; char nome_proprietario[30]; char cognome_proprietario[30]; char nome_cane [40]; int anni_cane; int razza_cane;} Cliente;
typedef struct {
codeint codice_razza; char nome_razza[30]; float altezza_media; float peso_medio;}Razza;
int leggiClienti( FILE* fp, Cliente* clienti); void StampaCliente (FILE * fps, Cliente* clienti, int dim, char cane[]); int leggiRazze (FILE* fr, Razza* razze); void StampaRazza (FILE* fpr, Razza* razze, int dim, char razza[]); Razza cercaRazza (Razza* razze, int dim_razze, Cliente* clienti);
int main(int argc, char *argv[]) {
codeCliente* clienti; Razza* razze; FILE* fp; FILE* fps; FILE* fr; FILE* fpr; int i = 0; int r = 0; char riga[100]; char stringa[100]; int dim_clienti, dim_razze; char cane[10]; char razza [20]; char razza_trovata[20]; fp = fopen ("C:\\Users\\Utente\\Desktop\\Esami EOL informatica 1\\Esame_Gennaio_2023\\Startkit\\clienti.txt", "r"); if (fp == NULL) { return -1; fprintf (stderr,"Errore: apertura file non consentita!\n"); } fps = fopen ("Stampa_clienti.txt", "w"); if (fps == NULL) { return -2; fprintf (stderr,"Errore: apertura file non consentita!\n"); } fr = fopen ("C:\\Users\\Utente\\Desktop\\Esami EOL informatica 1\\Esame_Gennaio_2023\\Startkit\\razze.txt", "r"); if (fr == NULL){ return -3; fprintf (stderr, "Errore: impossibile aprire il file!\n"); } fpr = fopen ("Stampa_razze.txt", "w"); if (fpr == NULL) { return -4; fprintf (stderr,"Errore: apertura file non consentita!\n"); } //per capire quanta memoria allocare leggo il file dei clienti a vuoto ed incremento le righe while (fgets (riga, 100, fp) != NULL){ i++; } clienti = malloc (i* sizeof (Cliente)); rewind (fp); // ritorno all'inizio del file dim_clienti = leggiClienti (fp, clienti); fclose (fp); - Assistant
In the given code, there are a few issues and improvements that can be made:
-
In the main function, the file paths for the input files ("clienti.txt" and "razze.txt") are hard-coded. It would be better to take the file paths as command-line arguments instead. You can modify the "argc" and "argv[]" parameters of the main function to achieve this.
-
There is a potential issue with the memory allocation for the "clienti" and "razze" arrays. The size of the arrays is determined based on the number of lines in the input files. However, it is assumed that each line in the input files corresponds to a valid entry in the arrays. If there are any invalid or empty lines in the input files, the allocated memory might be larger than necessary. It would be better to dynamically allocate memory as needed using realloc().
-