scanf() variable length specifier

2019-01-15 00:20发布

问题:

How can I use a variable to specify the max number of chars scanf() should read in?

For example using printf() you can use the * like so

#define MAXVAL 5
printf("Print at maximum MAXVAL chars: %.*s\n", MAXVAL, "myStringHere");

This will only print 5 chars, how can I make scanf only read in MAXVAL? MAXVAL must be used as the length specifier. I cannot simply do

scanf("%5s", string);

Right now I can only think of reading into a large array using scanf then using ssprintf to store the string into my length limited string. Using a length specifier would be so much easier however.

回答1:

You can use the C preprocessor to help you with that.

#define STR2(x) #x
#define STR(X) STR2(X)
scanf("%" STR(MAXVAL) "s", string);

The processor combines "%" STR(MAXVAL) "s" to "%5s"



回答2:

#include <stdio.h>

#define MAXLEN 5
#define S_(x) #x
#define S(x) S_(x)

int main(void){
    char string[MAXLEN+1];

    scanf("%" S(MAXLEN) "s", string);
    printf("<%.*s>\n", MAXLEN, string);
    return 0;
}


回答3:

You can't. You need to use something other than scanf(). A good and popular choice is fgets(), although its semantics are slightly different: fgets() will read a line of input, whereas scanf() with %s will read whitespace separated sequences of characters.

To use fgets(), you'd want something like:

fgets(string, MAXVAL, stdin);

If for some reason you really want to use scanf(), have a look at this question: How to prevent scanf causing a buffer overflow in C?



回答4:

Kernighan and Pike recommend using snprintf() to create the format string. I developed this idea into a method that safely reads strings:

void scan_string(char * buffer, unsigned length) {
    char format[12]; // Support max int value for the format %<num>s
    snprintf(format, sizeof(format), "%%%ds", length - 1); // Generate format
    scanf(format, buffer);
}

int main() {
  char string[5];
  scan_string(string, sizeof(string));
  printf("%s\n", string);
}


标签: c scanf