ciao. Il programma compila ma purtroppo nel momento in cui dovrebbe stampare a video gli utenti il cui nome inizia con "me" non funziona, così come non stampa a video gli utenti che hanno un punteggio maggiore di una soglia minima inseriti da input.
codice:#include <stdio.h> #include <string.h> #include <stdlib.h> #define DIM 32 typedef struct { char name[DIM]; int points; } user; int readPoints (char usersFile[], user results[], int maxDim, int minPoints); int readPoints (char usersFile[], user results[], int maxDim, int minPoints){ FILE *f; int i=0, stop=0, temp_1=0; char temp[33]; f= fopen(usersFile, "wb"); if (f == NULL) return -1; do{ printf ("Inserire il nome: "); scanf("%s", temp); if (strcmp(temp,"fine")==0) stop= 1; else fwrite(temp, sizeof(char[33]), 1, f); printf("Inserire il punteggio: "); scanf("%d", &temp_1); fwrite(&temp_1, sizeof(int), 1, f); } while (!stop && (strcmp(temp,"fine") != 0)); while (i<maxDim && (fread(&results[i], sizeof(user), 1, f)>0)){ /* fin tanto che i è minore della dim max inserita da utente e c'è qualcosa da leggere */ if (results[i].points >= minPoints) printf ("s", results[i].name); i++; } fclose(f); return i; } int main() { user *v; int x=0, y=0, l=0, i; printf ("Inserire il numero di clienti salvati su file: "); scanf ("%d", &x); v= (user*) malloc(sizeof(user)*x); printf ("Inserire il punteggio minimo: "); scanf ("%d", &y); l= readPoints("punti.dat", v, x, y); for (i=0; i<l; i++){ if ((v[i].name[0] == 'm') && (v[i].name[1] == 'e')) printf("L'utente %s ha %d punti.\n", v[i].name, v[i].points);} free (v); return 0; }

Rispondi quotando