我正在为C ++中的DirectX项目编写一个简单的Wavefront格式加载器。问题是我遇到了一个小而困难的问题。 fscanf破坏加载的数据到变量。加载THIS文件时,输出变量vt.y在第13行损坏,如下所示:
vt 0.500000 0.000087
下面给我带来如此麻烦的代码。我尝试了两个解决方案,首先是读取该行,然后尝试使用sscanf解析它,但它失败了,尽管输入fgets抓取完全没问题。第二个使用fscanf,但也失败了。
// earlier declarations:
float d1 = 0.0f, d2 = 0.0f;
...
else if ( strcmp ( "vt", (const char*)line ) == 0 )
{
char temp[256];
XMFLOAT2 vt;
//the 1st method: DON'T USE THEM BOTH TOGETHER, ONE OF THEM MUST CE COMMENTED OUT
fgets ( temp, sizeof( temp ), f ); // Result is correct: " 0.500000 0.000087\n"
sscanf ( temp, "%f %f", &d1, &d2 ); // d1 = 0.500000, d2 = 8.70000003e-005
//the 2nd method: DON'T USE THEM BOTH TOGETHER, ONE OF THEM MUST CE COMMENTED OUT
fscanf_s ( f, "%f %f", &vt.x, &vt.y ); // vt.x = 0.500000, vt.y = 8.70000003e-005
tex.push_back ( vt );
}
代码用标志编译:
#define _CRT_SECURE_NO_DEPRECATE
感谢您的帮助。