CODE
typedef struct { int id;
[tab][/tab][tab][/tab][tab][/tab]char password[5],name[30];
[tab][/tab][tab][/tab][tab][/tab]}clients;
[tab][/tab][tab][/tab][tab][/tab]char password[5],name[30];
[tab][/tab][tab][/tab][tab][/tab]}clients;
Thats the structure for a client
CODE
void add_client()
{FILE *file;
clients client;
file=fopen("clients.dat","r");
int i=0;
fread(&client,sizeof(clients),1,file);
while(!feof(file))
{i++;
fread(&client,sizeof(clients),1,file);
}
client.id=i;
printf("Your id will be:%d\n",client.id);
fclose(file);
printf("Name:");
gets(client.name); flushall();
printf("Enter a 4 character password:");
i=0;
while(i<4)
{flushall();
client.password[i]=getch();
printf("*");
i++;} client.password[4]=0;
file=fopen("clients.dat","a");
fwrite(&client,sizeof(clients),1,file);
}
{FILE *file;
clients client;
file=fopen("clients.dat","r");
int i=0;
fread(&client,sizeof(clients),1,file);
while(!feof(file))
{i++;
fread(&client,sizeof(clients),1,file);
}
client.id=i;
printf("Your id will be:%d\n",client.id);
fclose(file);
printf("Name:");
gets(client.name); flushall();
printf("Enter a 4 character password:");
i=0;
while(i<4)
{flushall();
client.password[i]=getch();
printf("*");
i++;} client.password[4]=0;
file=fopen("clients.dat","a");
fwrite(&client,sizeof(clients),1,file);
}
Thats the function to get a client and add it to the file, i believe the most important part is the password part.
CODE
int login()
{int loginid,record,i=0; char password[4];
do{puts("Input id:");
scanf("%d",&loginid);
}while(loginid<0); /*temp. fixing memory bug*/
puts("Input password"); flushall();
while(i<4)
{flushall();
password[i]=getch();
printf("*");
i++;} password[4]=0;
FILE *file;
clients client;
file=fopen("clients.dat","r");
i=0;
fread(&client,sizeof(clients),1,file);
while(!feof(file))
{printf("ID:%d Password:%s",client.id,client.password);
if((client.id==loginid)&&(!strcmp(client.password,password))){i=1; break;}
fread(&client,sizeof(clients),1,file);
}
if((client.id==loginid)&&(!strcmp(client.password,password)))i=1;
if(i) return(loginid);
return(0);
}
{int loginid,record,i=0; char password[4];
do{puts("Input id:");
scanf("%d",&loginid);
}while(loginid<0); /*temp. fixing memory bug*/
puts("Input password"); flushall();
while(i<4)
{flushall();
password[i]=getch();
printf("*");
i++;} password[4]=0;
FILE *file;
clients client;
file=fopen("clients.dat","r");
i=0;
fread(&client,sizeof(clients),1,file);
while(!feof(file))
{printf("ID:%d Password:%s",client.id,client.password);
if((client.id==loginid)&&(!strcmp(client.password,password))){i=1; break;}
fread(&client,sizeof(clients),1,file);
}
if((client.id==loginid)&&(!strcmp(client.password,password)))i=1;
if(i) return(loginid);
return(0);
}
Thats the function which asks for a client's id and password and searches the clients' file for them. Even though the password and id i enter match it never returns 1. Can someone plz tell me where is the error and in which function. I am assuming since the conditions don't look wrong it has to do with the password getting part.
Edit:
CODE
file=fopen("clients.dat","r");
int i=0;
fread(&client,sizeof(clients),1,file);
while(!feof(file))
{i++;
fread(&client,sizeof(clients),1,file);
}
client.id=i;
printf("Your id will be:%d\n",client.id);
fclose(file);
int i=0;
fread(&client,sizeof(clients),1,file);
while(!feof(file))
{i++;
fread(&client,sizeof(clients),1,file);
}
client.id=i;
printf("Your id will be:%d\n",client.id);
fclose(file);
This part reads how many records are inside the file, it used to work but for some weird reason it doesn't now, can someone tell me a possible reason? the only change that happened is that the previous client file was deleted.

