Devo svolgere delle operazioni su un file. In particolare devo poterci scrivere e leggerlo per effettuare confronti.
E' legato al progetto che sto facendo in cui ciò che scrivo sul file sono utenti (struct) caratterizzati da un nome utente, password ed un permesso (int). Prima di scrivere un utente sul file "login" devo verificare che non sia già presente un utente con quel nome. Il tutto l'ho svolto così ma.. non funziona:
//Registrazione Utente
int registraUtente(Utente utente) {
//Cerco se utente è già presente, in tal caso ritorna 0
Utente temp = cercaNome(utente);
if(strcmp(utente.nickname,temp.nickname) == 0) { //Nickname già scelto printf("Nome utente non disponibilen"); return -1;
}
int ds = open("login",O_WRONLY|O_APPEND); if(ds == -1) { return -1;
}
int w = write(ds,&utente,sizeof(utente));
if(w == -1) { return -1;
}
close(ds);
return 1;
}
//Cerca se il nome dell'utente è già presente
Utente cercaNome(Utente utente) {
Utente temp;
memset(temp.nickname,0,sizeof(temp.nickname));
memset(temp.password,0,sizeof(temp.password));
int cont = 0;
//Apro il file login
FILE *f;
f = fopen("login","r"); if(f == NULL) { printf("Errore apertura elenco utentin"); chiudiServer();
}
//Conteggio gli utenti presenti
else {
while(fscanf(f,"%s %s %d",temp.nickname,temp.password,&temp.permesso) == -1) { if(errno != EINTR) { printf("nErrore fscanf 1n"); chiudiServer();
}
}
while(!feof(f)) { while(fscanf(f,"%s %s %d",temp.nickname,temp.password,&temp.permesso) == -1) { if(errno != EINTR) { printf("nErrore fscanf 2n"); chiudiServer();
}
}
++cont;
}
printf("Numero utenti presenti: %dn", cont);
rewind(f); //Riposizionamento indice del file
//Ricerca dell'utente
int i;
for(i=0; i