我可以使用 scanf 捕获由变量指定宽度的指令吗?

发布于 2024-08-07 20:45:54 字数 325 浏览 1 评论 0原文

我有以下代码:

scanf(" %Xs %Ys", buf1, buf2);

其中 X 和 Y 应该是整数。问题是 X 和 Y 的值是编译时常量,即使我想将这些值硬编码到格式字符串中,我也做不到,因为我不知道这些值。在 printf 中,您可以发送宽度变量以及带有“%*s”的参数。有什么与 scanf 类似的东西吗?

编辑:澄清一下,常量在编译时是已知的,但在编码时不知道,而且我根本不知道。它们可能会因平台或实现而异,并且在我完成后它们可能会发生变化。即使他们没有,我仍然不希望在格式字符串中重复缓冲区大小,准备好在我忘记保持它们同步时出现段错误。

I have the following code:

scanf(" %Xs %Ys", buf1, buf2);

Where X and Y should be integers. The problem is that the values for X and Y are compile-time constants, and even if I wanted to hard-code the values into the format string, I can't, because I don't know the values. In printf, you can send a width variable along with the arguments with "%*s". Is there anything analogous for scanf?

EDIT: To clarify, constants are known at compile time, but not at coding time, and not by me at all. They may vary by platform or implementation, and they may change after I'm done. Even did they not, I still wouldn't want to have buffer sizes duplicated in format strings, ready to segfault the minute I forget to keep them synchronized.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江挽川 2024-08-14 20:45:54

您可以使用 sprintf() 生成格式字符串:

sprintf( format, " %%%is %%%is", X, Y );
scanf(format, buf1, buf2);

编辑:太棒了,但是以下 gcc 代码正在工作:

#include <stdio.h> 

#define LIST(...) __VA_ARGS__ 

#define scanf_param( fmt, param, str, args ) {  \ 
  char fmt2[100]; \ 
  sprintf( fmt2, fmt, LIST param ); \ 
  sscanf( str, fmt2, LIST args  ); \ 
} 

enum { X=3 };
#define Y X+1 

int main(){
  char str1[10], str2[10];

  scanf_param( " %%%is %%%is", (X,Y), " 123 4567", (&str1, &str2) );

  printf("str1: '%s'   str2: '%s'\n", str1, str2 );
}

You may produce the format string with sprintf():

sprintf( format, " %%%is %%%is", X, Y );
scanf(format, buf1, buf2);

EDIT: amazing, but the following gcc code is working:

#include <stdio.h> 

#define LIST(...) __VA_ARGS__ 

#define scanf_param( fmt, param, str, args ) {  \ 
  char fmt2[100]; \ 
  sprintf( fmt2, fmt, LIST param ); \ 
  sscanf( str, fmt2, LIST args  ); \ 
} 

enum { X=3 };
#define Y X+1 

int main(){
  char str1[10], str2[10];

  scanf_param( " %%%is %%%is", (X,Y), " 123 4567", (&str1, &str2) );

  printf("str1: '%s'   str2: '%s'\n", str1, str2 );
}
只是一片海 2024-08-14 20:45:54

问题是 X 的值
和 Y 是编译时常量

然后使用宏粘贴功能:

#include <stdio.h>

#define TEST 2

#define CONST_TO_STRING_(x) #x
#define CONST_TO_STRING(x) CONST_TO_STRING_(x)

int main() {
    printf("1 " CONST_TO_STRING(TEST) " 3\n");
    return 0;
}

The problem is that the values for X
and Y are compile-time constants

Then use macro paste feature:

#include <stdio.h>

#define TEST 2

#define CONST_TO_STRING_(x) #x
#define CONST_TO_STRING(x) CONST_TO_STRING_(x)

int main() {
    printf("1 " CONST_TO_STRING(TEST) " 3\n");
    return 0;
}
复古式 2024-08-14 20:45:54

你的问题不清楚。如果您不知道这些值,那么它们可能是运行时常量,而不是编译时常量。

如果是这种情况(即它们是运行时常量),那么不,“scanf”中没有这样的功能。您唯一能做的就是在运行时使用“sprintf”生成完整的格式字符串(其中嵌入了具体值),然后将该格式字符串传递给“scanf”。

Your question is not clear. If you don't know the values, then they are probably run-time constants, not compile-time constants.

If that's the case (i.e. they are run-time consants), then no, there's no such feature in 'scanf'. The only thing you can do is to produce the complete format string (with the concrete values embedded in it) at run-time by using 'sprintf' and then pass that format string to your 'scanf'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文