Ho una struct Utente...
codice:
typedef struct {
char nickname[NICKNAME];
char password[PASSWORD];
int permesso;
}Utente;
Ho un file che contiene vari utenti.
Devo creare una fuzione che prende in input UN utente e mi dice se è già presente nel file.
Ho buttato giù una cosa del genere:
codice:
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"); //posizionato nella medesima cartella del programma..
if(f == NULL) {
printf("Errore apertura elenco utenti\n");
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 1\n");
chiudiServer();
}
}
while(!feof(f)) {
while(fscanf(f,"%s %s %d",temp.nickname,temp.password,&temp.permesso) == -1) {
if(errno != EINTR) {
printf("\nErrore fscanf 2\n");
chiudiServer();
}
}
++cont;
}
printf("Numero utenti presenti: %d\n", cont);
rewind(f); //Riposizionamento indice del file
//Ricerca dell'utente
int i;
for(i=0; i<cont; i++) {
while(fscanf(f,"%s %s %d",temp.nickname,temp.password,&temp.permesso) == -1) {
if(errno != EINTR) {
printf("\nErrore fscanf 3\n");
chiudiServer();
}
}
if(strcmp(utente.nickname,temp.nickname) == 0) { //Utente trovato
while(fclose(f) != 0) {
if(errno != EINTR) {
printf("\nErrore fclose 1\n");
chiudiServer();
}
}
return temp;
}
}
while(fclose(f) != 0){
if(errno != EINTR){
printf("\nErrore fclose 2\n");
chiudiServer();
}
}
Utente vuoto = { "NULL", "NULL", 0 }; //Utente non trovato
return vuoto;
}
}
Ma ovviamente non funziona, non conta nemmeno gli utenti ed esce la scritta
smash stack detected, una cosa simile..