我写了一个简短的示例代码来说明我的问题
#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
但我就是不明白。我错过了一些概念。
有人可以提供universalFormat
适合此示例的内容并解释如何到达那里。