我正在尝试读取一个格式为:
的文件。 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]
会跳过非浮点数的想法的? - Steve Summitfgets
读取整行,然后再用sscanf
提取三个数字。 - user3386109