dear friends,
past 5 hours i working out this problem, here a confliction between scanf and fgets...
may be i m using wrong term conflict, but i m going to explain you the exact problem..
just look at the code segment, i will explain in code itself with the help of comments
please consider the declaration of all variable...
Code:
for reference
here is the structure
typedef struct
6 {
7
8 char name[10];
9 char phone[10];
10 char address[20];
11 char amount[10] ;
12 int inactive ;
13
14 } RECORD ;
Code:
printf("Account Detail change form\n");
23 printf("Type your account no :") ; // promt
24 scanf("%d", &accno) ; // here taking input from user into accno
25 fd = open("dbase.dat",O_RDWR); // opening database file ( can be omit)
26 lseek(fd,(accno -1)*sizeof(RECORD),SEEK_SET) ; // seeking the record ( can be omit)
27 //set lock
28 lockf(fd,sizeof(RECORD),F_LOCK) ; // setting lock ( can be omit)
29 //end
30 rec = (RECORD *)malloc(sizeof(RECORD)) ; // mallocing the memory to RECORD
31 read(fd,rec,sizeof(RECORD)) ; // reading info from record ( can be omit)
32 printf("Info in dbase as follows :\n") ;
33 printf("Address : %s",rec->address) ;
34 printf("Phone : %s", rec->phone) ;
35 printf("**********NOW enter the new info*************\n") ;
36 printf("Address :");// here all the problem starts
37 fgets(rec->address,20,stdin) ;// when i run the program my prog skip this and not take
// any input into rec->address, and jump to next promt...
38
39 printf("Phone Number :") ; //printf this
40 fgets(rec->phone,10,stdin) ; // and jump here and ask for input
// and leave rec->address array empty
41 printf("******** Thank you *******\n") ;
42 write(fd,rec,sizeof(RECORD)) ;
I have guess that when i giving input to var accno, and pressing enter to tell prog that i hav enter the input, the enter is still in the keyboard buffer, stdin , and first fgets is taking it and going to second fgets.
tell if my gusss is right, tell how can i resolve it, if not tell what you think and how can i resolve it.
if still problem is not clear you can ask for more clearification.
thankx