0

This is the partial code:

typedef struct { int dd, mm, yy; } dateType; typedef struct { char username[50], password[50]; int acctype; dateType date; } usertype; typedef struct { float transactions; char use[50]; dateType date; } transactype; typedef struct node { usertype employee[2]; usertype admin; transactype trans[3]; float funds; int department; struct node *next; } nd; void logon(nd **); void admin_reg(nd **); void main(void) { int choice; nd *head, *p; do { printf("MENU:\n"); printf("1.Log In\n"); printf("2.Admin/Manager Registration\n"); printf("3.Exit\n"); printf("Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: logon(&head); clrscr(); break; case 2: admin_reg(&head); clrscr(); break; case 3: exit(1); break; default: printf("Invalid choice.\n"); break; } }while(choice!=3); } void admin_reg(nd **head) { int i; nd *p; FILE *fp; if((fp=fopen("Admin.txt","w"))==NULL) { printf("file not found"); exit(1); } *head = (nd*)malloc(sizeof(nd)); printf("admin username: "); scanf("%s",(*head)->admin.username); printf("admin password: "); scanf("%s",(*head)->admin.password); printf("Admin department:1 2 3: "); scanf("%d",&(*head)->department); (*head)->admin.acctype=3; fwrite(*head, sizeof(nd), 1, fp); p = *head; for(i = 2; i <= 3; i++) { p->next = (nd*)malloc(sizeof(nd)); printf("admin username: "); scanf("%s",p->next->admin.username); printf("admin password: "); scanf("%s",p->next->admin.password); printf("Admin department:1 2 3: "); scanf("%d",&p->next->department); p->next->admin.acctype=3; fwrite(p->next,sizeof(nd),1,fp); p = p->next; } p->next = NULL; fclose(fp); } void logon(nd **head) { nd *p; char username[50], password[50]; p=*head; FILE *fp; if((fp=fopen("Admin.txt","r"))==NULL) { printf("file not found"); exit(1); } printf("Input username:"); scanf("%s",username); printf("Input password:"); scanf("%s",password); while(fread(p, sizeof(nd), 1, fp)==1) { if(strcmp(p->admin.username,username)==0 && strcmp(p->admin.password,password)==0) { puts(p->admin.username); puts(p->admin.password); printf("\nSuccessfully compared!");getch(); } /* else { for(x=0;x<2;x++) if(strcmp(username,p->employee[x].username)==0 && strcmp(password,p->employee[x].password)==0) { y++; } } */ p=p->next; } fclose(fp);getch(); } 

I'm getting an error on the fread part, fwrite is fine. When I try to check if the 2nd or 3rd account could be retrieved the whole program just stops, 1st account seems fine, 2nd and 3rd not.

EDIT: Thanks for those who answered, I finally found the source of my problem, You've all been very helpful. Thanks for the advice. :)

5
  • 1
    Don't use void main. main must be declared int. Commented Oct 10, 2013 at 16:02
  • 1
    Also, don't cast the result of malloc. Commented Oct 10, 2013 at 16:11
  • you are most likely overwriting something in memory. Nobody is going to wade through pages of code. If you don't know how to use a debugger, I would first recommend getting familiar with your IDE and stepping throught the code with a debugger. Commented Oct 10, 2013 at 16:13
  • since c98 standard, ` void main` is a error. or c99. Commented Oct 10, 2013 at 16:13
  • Thanks for the help, though I probably should have stated that I'm using an old compiler so not casting malloc gives me an error, I did try changing void main to int main but that didn't alleviate the problem :(. Thank you for replying though :), the error only occurs during the logon function, when I try to login using the 2nd or 3rd accounts that were supposed to be created and stored in the file. fread seems to retrieve the 1st account fine, the 2nd and 3rd.. not so much. Commented Oct 10, 2013 at 16:18

1 Answer 1

1

You fread into p but never allocate memory for it, either change p to

nd p; 

and pass &p as the first argument to fread, or malloc memory for it:

nd * p = malloc(sizeof(nd)); 

EDIT: I see that you assign p = *head; in logon and pass head in from main, so the code in logon should work but head isn't allocated in main. So change nd * head to nd head or malloc it.

Sign up to request clarification or add additional context in comments.

1 Comment

so I shouldn't use p=*head;? head was passed and already has an allocated memory though, I'll try your suggestion, thank you :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.