Thread: Is it conflict between scanf and fgets

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Question Is it conflict between scanf and fgets

    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
    Last edited by alokdotnet; 08-18-2006 at 09:31 PM. Reason: addiong more code

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't mix input methods. Stick to one or the other. Consider using fgets with sscanf.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Yes scanf will leave the newline in the buffer. The FAQ has some solutions. I prefer this;
    Code:
    int c;
    while ((c = getchar()) != '\n' && c != EOF);

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    Quote Originally Posted by quzah
    Don't mix input methods. Stick to one or the other. Consider using fgets with sscanf.


    Quzah.
    Dear Quzah, sscanf has following protype
    Code:
    int sscanf(const char *str, const char *format, ...);
    if i m using it then i have to pass char * as first argument, but here i want to take input as integer, then how i will do it.
    plz clearify it.

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    if i m using it then i have to pass char * as first argument, but here i want to take input as integer, then how i will do it.
    plz clearify it.
    Use atoi() on the string or similar.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You already know how to use scanf, what Quzah suggested is similar. Get a string from the user first: sscanf checks a string for a format instead of what the user directly inputs.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Question

    Quote Originally Posted by OnionKnight
    Use atoi() on the string or similar.
    But friend problem is here, to convert integer to string, and atoi changes ascii to integer...
    another function is there itoa(), but his is not standard ANSI function, and not supported by GCC.
    i can use sprintf for that same purpose, but here it not as requirnment, if you have seen the code, just below i have several operation where i m useing accno for comparisions.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Same thing, over and over:
    User Input: Strings and Numbers [C]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Quote Originally Posted by alokdotnet
    But friend problem is here, to convert integer to string, and atoi changes ascii to integer...
    another function is there itoa(), but his is not standard ANSI function, and not supported by GCC.
    i can use sprintf for that same purpose, but here it not as requirnment, if you have seen the code, just below i have several operation where i m useing accno for comparisions.
    I am not sure what you're asking, and I see no comparisons done with accno.
    The problems I can see with the code is the scanf/fgets intervention and that you're overwriting the account after the current one in your database file.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    char strnum[20] ;
    int accno;
    ......
    fgets(strnum,sizeof strnum,stdin);
    sscanf(strnum,"%d",&accno) ;
    ......

    by this my problem is solved....
    good way to come out such problems..

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > rec = (RECORD *)malloc(sizeof(RECORD)) ;
    See the FAQ on casting malloc as well.

    Also, can you stop line-numbering your code, it makes it too hard to copy/paste to an editor.
    If you want to highlight specific lines, then use a /* this line */ comment, and refer to that in your text.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf vs fgets?
    By Matus in forum C Programming
    Replies: 65
    Last Post: 11-17-2008, 04:02 PM
  2. is scanf as safe as fgets when used to receive string?
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 08-31-2005, 05:18 PM
  3. replacing scanf with fgets
    By subflood in forum C Programming
    Replies: 6
    Last Post: 08-19-2004, 01:59 PM
  4. replacing scanf with fgets
    By guest73 in forum C Programming
    Replies: 8
    Last Post: 09-16-2002, 04:52 AM
  5. String manipulation and scanf vs fgets
    By Nit in forum C Programming
    Replies: 9
    Last Post: 03-20-2002, 12:44 PM