scanf的格式忽略不相关的字符 [英] scanf format to ignore irrelevant characters

查看:191
本文介绍了scanf的格式忽略不相关的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的例子code来说明我的问题。

I wrote a short example code to illustrate my problem

#include <stdio.h>
#include <string.h>

unsigned parseAndCompareDouble(const char* inSTR, const char* inF, const char * expect, const char * outF){
    unsigned e = 0;
    char buffer[2000];
    double a = 0;
    if( 1 != sscanf_s( inSTR, inF, &a, sizeof(double) ) ) e += 1;
    if( (int) strlen(expect) != sprintf_s(buffer, 2000, outF, a) ) e += 1;
    if( 0 != strcmp(expect, buffer) ) e += 1;
    return e;
}

unsigned main( void )
{
    unsigned e = 0;
    const char * universalFormat = "X/%lf";

    e += parseAndCompareDouble("X/100", universalFormat, "X/100", "X/%3.0lf");
    e += parseAndCompareDouble("     X/100\r\n", universalFormat, "X/100", "X/%3.0lf");
    e += parseAndCompareDouble("     X/99\r\n", universalFormat, "X/99", "X/%2.0lf");
    e += parseAndCompareDouble("     X / 99 ", universalFormat, "X/99", "X/%2.0lf");
    e += parseAndCompareDouble("X/99", universalFormat, "X/99", "X/%2.0lf");
    e += parseAndCompareDouble("     \"X/100\"\r\n", universalFormat, "X/100", "X/%3.0lf");  

    if( 0 != e ){ printf( "%2u errors occured\n", e ); }
    else{ printf( "all pass\n", e ); }
    return e;
}

我要寻找一个 universalFormat ,让我的例子夹具通过测试。我试着摆弄%* S ,但我只是不明白这一点分辩。我错过了一些概念。

I am looking for a universalFormat that lets my example fixture pass the test. I tried to fiddle around with %*s but I just don't get it rigth. I am missing some concept.

有人可以提供 universalFormat 适合这个例子,并解释如何到达那里。

Can someone provide the universalFormat that fits this example and explain how to get there.

推荐答案

您可以使用以下格式:

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;

这篇关于scanf的格式忽略不相关的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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