Thread: replacing scanf with fgets

  1. #1
    guest73
    Guest

    replacing scanf with fgets

    Dear All,

    I realise there are many threads concerning the use of fgets, however I am still having difficulty after having read many of the previous threads!

    Put simply, I have to replace every use of scanf in my code with fgets. For example, here is a typical example of the use of scanf in the current code:

    /* example */
    Code:
       
    int quest;
    
       printf("Are you sure you wish to exit the Menu?\n");
       printf("Press 1) to return to Options or 2) to exit the Menu: ");
       scanf("%d",&quest);
    /* end example */

    Any ideas how I replace scanf with the use of fgets? Any advice would be greatly appreciated.

    Cheers.....


    Code tags added by kermi3

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    What exactly are you having difficulty with.

    char* fgets(char* string,int number,FILE* stream)

    fgets will retrieve number bytes from stream and place in buffer pointed to by string. To read from the keyboard make stream stdin.Because you control the number of bytes read there is no chance of your buffer overrunning and causing problems.
    Next to parse the string use sscanf(). This works like scanf() except instead of taking its input from the keyboard it will take its input from the buffer filled by your fgets call.
    There are a multitude of code snippets illustrating this on this site. Look at a few of them.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    In the furture please use Code Tags.

    Information on code tags may be found at:
    http://www.cprogramming.com/cboard/...&threadid=13473
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Code:
    int quest;
    char int_holder[10];
    
    printf("Are you sure you wish to exit the Menu?\n");
    printf("Press 1) to return to Options or 2) to exit the Menu: ");
    quest = atoi( fgets( int_holder, 10, stdin ) );
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >quest = atoi( fgets( int_holder, 10, stdin ) );
    Not the best idea... what happens if fgets() returns NULL? Crash and burn me thinks...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    guest73
    Guest

    Smile

    thanks for your replies everyone – I shall try to implement the suggested solutions this evening and let you know how I go.

    as for code tags – the link you provided is no longer working. where can I find this information?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >as for code tags – the link you provided is no longer working. where can I find this information?
    http://www.cprogramming.com/cboard/s...threadid=13473
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    guest73
    Guest

    Thumbs up

    much appreciated hammer ..... code tags will now become part of the routine.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >much appreciated hammer
    No problem!

    >code tags will now become part of the routine.
    Nice to hear
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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...
    By 8ball in forum C Programming
    Replies: 8
    Last Post: 05-15-2004, 08:45 PM
  5. String manipulation and scanf vs fgets
    By Nit in forum C Programming
    Replies: 9
    Last Post: 03-20-2002, 12:44 PM