且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

scanf的格式忽略不相关的字符

更新时间:2023-02-09 21:24:22

您可以使用以下格式:

const char * universalFormat = "%*[^/]/%lf";

%* [^ /] 告诉 scanf函数无视一切,这是不是一个 / 。我假设你的 sscanf_s 函数会明白,虽然我无法证实自己。 这里的工作程序(略有修改)。

The %*[^/] tells scanf to ignore everything that isn't a /. I am assuming your sscanf_s function will understand it, although I am unable to verify that myself. Here's the working program (slightly modified).

丹尼尔·菲舍尔带来了我的注意,在 sscanf_s sprintf_s 函数在C标准的附录K定义2011(C11)。 我提出有关它关系到一致性的问题。

Daniel Fischer has brought to my attention that the sscanf_s and sprintf_s functions are defined in Annex K of C Standard 2011 (C11). I raised a question about its relation to conformance.

知道了X /是字符串中是很重要的我。

Knowing that "X /" is in the string is of importance for me.

看来你要使用的sscanf 解析***格式输入,这是不是真的它的拿手好戏的。如果你愿意改变你解析code,则可以使用格式字符串的修改版本来实现:

It seems you are trying to use sscanf to parse free-form input, which is not really its forte. If you are willing to change your parsing code, you can use the modified version of the format string to accomplish this:

const char * universalFormat = "%[^/]/%lf";

现在,您解析code将需要更新相应的%[^ /] 说明字符串中的阅读,那么你可以做一些它简单的扫描,以确保它符合您的要求。

Now, your parsing code will need to be updated to read in the string corresponding to the %[^/] specifier, and then you can do some simple scanning on it to make sure it meets your requirements.

char xbuffer[2000];
const char *xp;
/*...*/
if( 2 != sscanf_s( inSTR, inF, xbuffer, sizeof(xbuffer), &a, sizeof(a) ) ) e += 1;
if( (xp = strrchr(xbuffer, 'X')) == 0 || strcspn(xp, "X \t") != 0 ) e += 1;