/* Nome: Mario Cognome: Montanari Classe: 3AIN Data: 21/03/2025 Aprire il file struct.txt con il blocco note, realizzare una funzione per ciascuna delle seguenti richieste: - leggere struct.txt e caricare i valori in un array di struct; - calcolare il rapporto fra il campo reddito e il campo età. Realizzare un main che utilizza le funzioni. */ #include #include #include #define SIZE 100 using namespace std; typedef struct { char nome[SIZE]; int eta; int reddito; char citta[SIZE]; } elementi; void leggiFileCaratterePerCarattere(const char *nomeFile) { FILE *fp = fopen(nomeFile, "rt"); if (fp != NULL) { elementi line; while (fscanf(fp, "%s %d %d %s", line.nome, &line.eta, &line.reddito, line.citta) == 4) { // Serve '&' quando usiamo int, float, double, char (se è un singolo carattere), etc. in una scanf cout << line.nome << " " << line.eta << " " << line.reddito << " " << line.citta << endl; } fclose(fp); } else { perror("Error (source)"); } } void rapportoRedditoEta(const char *nomeFile) { FILE *fp = fopen(nomeFile, "rt"); if (fp != NULL) { elementi line; float rapporto; cout << "Rapporti reddito/eta': " << endl; while (fscanf(fp, "%s %d %d %s", line.nome, &line.eta, &line.reddito, line.citta) == 4) { // Serve '&' quando usiamo int, float, double, char (se è un singolo carattere), etc. in una scanf rapporto = (float)line.reddito / line.eta; cout << " " << line.nome << ": " << rapporto << endl; } fclose(fp); } else { perror("Error (source)"); } } int main(void) { leggiFileCaratterePerCarattere("struct.txt"); cout << endl; rapportoRedditoEta("struct.txt"); return 0; }