scanf() 的宽度说明符 - 要使用的字符长度在编译时不固定,仅在运行时确定.怎么让它变呢? [英] 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?

查看:96
本文介绍了scanf() 的宽度说明符 - 要使用的字符长度在编译时不固定,仅在运行时确定.怎么让它变呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字段宽度说明符应用于 scanf() 操作以读取字符串,因为要明确指定要读取/使用的字符数量而不是使 scanf()-容易导致缓冲区溢出的操作.除了目标参数指向已经匹配的 char 数组,该数组与元素的大小完全相同,字段宽度的所需值必须是,对于 \0.这个char数组的大小也是在运行之前确定的.

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?

我做了一些研究,发现在 Stackoverflow 上已经有一个问题,在其来源中,解决了与我完全相同的问题.scanf() 可变长度说明符

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. 有没有办法使 scanf() 函数内的最大字段宽度取决于运行时确定或生成的值?

  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?

是否有其他方法可以实现相同的目标?

Is there an alternative to achieve the same?

我使用 C 和 C++ 并为两者标记问题,因为我不想为每个分开的问题重复相同的问题.如果两者之间的答案发生变化,请说明重点是哪种语言.

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.

推荐答案

您可以使用 sprintf 将其用作格式:

You can use sprintf to use it as a format:

只是为了评论,我使用了 unsigned 因为我无法想象字符串长度为负的情况.

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;
}

这篇关于scanf() 的宽度说明符 - 要使用的字符长度在编译时不固定,仅在运行时确定.怎么让它变呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆