Search Results

Search found 1 results on 1 pages for 'user1016401'.

Page 1/1 | 1 

  • Reading/Writing/Modifying a struct in C

    - by user1016401
    I am taking some information from a user (name, address, contact number) and store it in a struct. I then store this in a file which is opened in "r+" mode. I try reading it line by line and see if the entry I am trying to enter already exists, in which case I exit. Otherwise I append this entry at the end of the file. The problem is that when I open the file in "r+" mode, it gives me Segmentation fault! Here is the code: struct cust{ char *frstnam; char *lastnam; char *cntact; char *add; }; Now consider this function. I am passing a struct of information in this function. Its job is to check if this struct already exists else append it to end of file. void check(struct cust c) { struct cust cpy; FILE *f; f=fopen("Customer.txt","r+"); int num=0; if (f!= NULL){ while (!feof(f)) { num++; fread(&cpy,sizeof(struct cust),1,f); if ((cpy.frstnam==c.frstnam)&(cpy.lastnam==c.lastnam)&(cpy.cntact==c.cntact)&(cpy.add==c.add)) { printf("Hi %s %s. Nice to meet you again. You live at %s and your contact number is %s\n", cpy.frstnam,cpy.lastnam,cpy.add,cpy.cntact); return; } } fwrite(&c,sizeof(struct cust),1,f); fclose (f); } printf("number of lines read is %d\n",num); }

    Read the article

1