Width Specifier for scanf() - Length of characters to consume is not fixed at compilation and only determined at run-time. How to make it variable?

会有一股神秘感。 提交于 2019-12-11 03:55:56

问题


I want to apply the field-width specifier to the scanf()-operation for reading a string due to clearly specify the amount of characters to read/consume and not make the scanf()-operation susceptible for causing buffer overflow. As well as the destination argument points to an already matched char array, which has exactly the size of elements, the desired value of the field width has to be, + 1 for the \0. The size of this chararray is also determined at run-time before.

The problem now is that the value for the maximum field width can not be fixed; it is determined only at run-time.

How do i achieve, that the maximum field width´s value is able to be determined at run-time?


I did a bit of research and found, that there is a question already made here on Stackoverflow, that in its source, adresses the exact same question as i have. scanf() variable length specifier

BUT unfortunately inside the development of the question as well as inside the answers, the solutions turned out to be only handled with a Preprocessor-directive Macro, which means that the value for the field width is not actually that variable, it is fixed at compilation-time.


I have an example for you what i mean:

#include <stdio.h>

int main(void)
{
    int nr_of_elements;

    printf("How many characters your input string has?\n");
    scanf("%d",&nr_of_elements);

    nr_of_elements++;                          //+1 element for the NULL-terminator.

    char array[nr_of_elements];

    printf("Please input your string (without withspace characters): ");
    scanf("%s",array);        // <--- Here i want to use a field width specifier.      

    return 0;
}

What i would want to do is something like this:

scanf("%(nr_of_elements)s");

Or if i follow the programming style of the answers in the linked question:

scanf("%" "nr_of_elements" "s");

  1. Is there a way to make the maximum field width inside of the scanf()-function depended from a by run-time determined or generated value?

  2. Is there an alternative to achieve the same?

I use C and C++ and tag the question for both, because i did not wanted to duplicate the same question for each separated. If answers between those alter, please mention which language is at focus.


回答1:


You can use sprintf to use it as a format:

Just to comment, I used unsigned because I can't imagine a case when you will have a negative length of a string.

#include <stdio.h>

int main(void)
{
    unsigned nr_of_elements;

    printf("How many characters your input string has?\n");
    scanf("%u",&nr_of_elements);

    nr_of_elements++;                          //+1 element for the NULL-terminator.

    char array[nr_of_elements];

    printf("Please input your string (without withspace characters): ");

    char format[15]; //should be enough
    sprintf(format, "%%%us", nr_of_elements - 1);
    scanf(format,array);       

    return 0;
}


来源:https://stackoverflow.com/questions/59156783/width-specifier-for-scanf-length-of-characters-to-consume-is-not-fixed-at-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!