How to limit scanf function in C to print error when input is too long?

前端 未结 6 996
梦谈多话
梦谈多话 2020-12-11 07:17

I want to limit the scanf function so when I enter for example a char* array that has more then 30 characters, it will not get it and my outpu

6条回答
  •  抹茶落季
    2020-12-11 07:57

    Take a look at this page http://linux.die.net/man/3/sscanf and look for the %n format specifier. I would also recommend looking the sscanf function's return value, which will tell you the number of formatted arguments, as well as the presence of error.

    I've used the %n format specifier to help in parsing a string of parameters:

            ret = sscanf(line, "%d %d %s %d %d %n", &iLoad, &iScreen, &filename, &stage, &bitmapType, &offset);
    

    The number of chars formatted by the preceding arguments is stored in the variable offset.

提交回复
热议问题