简体   繁体   中英

Using scanf to accept user input

gcc 4.4.2

I was reading an article about scanf. I personally have never checked the return code of a scanf.

#include <stdio.h>

int main(void)
{
  char buf[64];
  if(1 == scanf("%63s", buf))
  {
    printf("Hello %s\n", buf);
  }
  else
  {
    fprintf(stderr, "Input error.\n");
  }
  return 0;
}

I am just wondering what other techniques experienced programmers do when they use scanf when they want to get user input? Or do they use another function or write their own?

Thanks for any suggestions,

EDIT =========

#include <stdio.h>

int main(void)
{
    char input_buf[64] = {0};
    char data[64] = {0};

    printf("Enter something: ");
    while( fgets(input_buf, sizeof(input_buf), stdin) == NULL )
    {
    /* parse the input entered */
    sscanf(input_buf, "%s", data);
    }

    printf("Input [ %s ]\n", data);

    return 0;
}

I think most programmers agree that scanf is bad, and most agree to use fgets and sscanf. However, I can use fgets to readin the input. However, if I don't know what the user will enter how do I know what to parse. For example, like if the user was to enter their address which would contain numbers and characters and in any order?

Don't use scanf directly. It's surprisingly hard to use. It's better to read an entire line of input and to then parse it (possibly with sscanf ).

Read this entry (and the entries it references) from the comp.lang.c FAQ: http://c-faq.com/stdio/scanfprobs.html

Edit: Okay, to address your additional question from your own edit: If you allow unstructured input, then you're going to have to attempt to parse the string in multiple ways until you find one that works. If you can't find a valid match, then you should reject the input and prompt the user again, probably explaining what format you want the input to be in.

For anything more complicated, you'd probably be better off using a regular expression library or even using dedicated lexer/parser toolkits (eg flex and bison).

I don't use scanf() for interactive user input; I read everything as text using fgets() , then parse the input as necessary, using strtol() and strtod() to convert text to numeric values.

One example of where scanf() falls down is when the user enters a bad numeric value, but the initial part of it is valid, something like the following:

if (scanf("%d", &num) == 1)
{
  // process num
}
else
{
  // handle error
}

If the user types in "12e4", scanf() will successfully convert and assign the "12" to num, leaving "e4" in the input stream to foul up a future read. The entire input should be treated as bogus, but scanf() can't catch that kind of error. OTOH, if I do something like:

if (fgets(buffer, sizeof buffer, stdin))
{
  int val;
  char *chk;
  val = (int) strtol(buffer, &chk, 10);
  if (!isspace(*chk) && *chk != 0)
  {
    // non-numeric character in input; reject it completely
  }
  else
  {
    // process val
  }
}

I can catch the error in the input and reject it before using any part of it. This also does a better job of not leaving garbage in the input stream.

scanf() is a great tool if you can guarantee your input is always well-formed.

scanf() has problems, in that if a user is expected to type an integer, and types a string instead, often the program bombs. This can be overcome by reading all input as a string (use getchar()), and then converting the string to the correct data type.

/* example one, to read a word at a time */
#include <stdio.h>
#include <ctype.h>
#define MAXBUFFERSIZE   80

void cleartoendofline( void );  /* ANSI function prototype */

void cleartoendofline( void )
{
    char ch;
    ch = getchar();
    while( ch != '\n' )
        ch = getchar();
}

main()
{
    char    ch;                     /* handles user input */
    char    buffer[MAXBUFFERSIZE];  /* sufficient to handle one line */
    int     char_count;             /* number of characters read for this line */
    int     exit_flag = 0;
    int     valid_choice;

    while( exit_flag  == 0 ) {
        printf("Enter a line of text (<80 chars)\n");
        ch = getchar();
        char_count = 0;
        while( (ch != '\n')  &&  (char_count < MAXBUFFERSIZE)) {
            buffer[char_count++] = ch;
            ch = getchar();
        }
        buffer[char_count] = 0x00;      /* null terminate buffer */
        printf("\nThe line you entered was:\n");
        printf("%s\n", buffer);

        valid_choice = 0;
        while( valid_choice == 0 ) {
            printf("Continue (Y/N)?\n");
            scanf(" %c", &ch );
            ch = toupper( ch );
            if((ch == 'Y') || (ch == 'N') )
                valid_choice = 1;
            else
                printf("\007Error: Invalid choice\n");
            cleartoendofline();
        }
        if( ch == 'N' ) exit_flag = 1;
    }
}

I make a loop call fgets until the end of the line is read, and then call sscanf to parse the data. It's a good idea to check whether sscanf reaches the end of the input line.

I rarely use scanf . Most of the times, I use fgets() to read data as a string. Then, depending upon the need, I may use sscanf() , or other functions such as strto* family of functions, str*chr() , etc., to get data from the string.

If I use scanf() or fgets() + sscanf() , I always check the return values of the functions to make sure they did what I wanted them to do. I also don't use strtok() to tokenize strings, because I think the interface of strtok() is broken.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM