我正在尝试读取具有以下格式的文件:
ID: x y z ...... other crap
第一行看起来像这样:
0: 0.82 1.4133 1.89 0.255 0.1563 armTexture.jpg 0.340 0.241 0.01389
我只需要x y z浮点数,其余的行就是垃圾。我的代码当前如下所示:
int i;
char buffer[2];
float x, y, z;
FILE* vertFile = fopen(fileName, "r"); //open file
fscanf(vertFile, "%i", &i); //skips the ID number
fscanf(vertFile, "%[^f]", buffer); //skip anything that is not a float (skips the : and white space before xyz)
//get vert data
vert vertice = { 0, 0, 0 };
fscanf(vertFile, "%f", &x);
fscanf(vertFile, "%f", &y);
fscanf(vertFile, "%f", &z);
fclose(vertFile);
为了调试它已经作了一些更改(最初的前两个scanf使用*来忽略输入)。
当我运行此命令时,x,y,z不会改变。如果我做到了
int result = fscanf(vertFile, "%f", &x);
结果为0,我相信这告诉我它根本没有将数字识别为浮点数吗?我尝试将xyz切换为double并也使用%lf,但这也不起作用。
我可能做错了什么?
[%[^f]
不会跳过非浮点,它会跳过非字母'f'
的所有字符。
改为尝试%*s
。 %s
读取直到下一个空格,然后*
放弃结果。