scanf format to ignore irrelevant characters

本秂侑毒 提交于 2019-12-05 17:07:08
jxh

You can use the following format:

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

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).


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.


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

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

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