我一直在构建一个程序,将一个模型文件类型转换为一个wavefront obj one,但是在将我的面部写入该文件时遇到了问题。
if(!strcmp(line , "TEX:TOP"))
{
i++;
TEX_TOP << "f " << i << "/" << i << "/" << i << " ";
i++;
TEX_TOP << TEX_TOP << i << "/" << i << "/" << i << " ";
i++;
TEX_TOP << TEX_TOP << i << "/" << i << "/" << i << " ";
i++;
TEX_TOP << TEX_TOP << i << "/" << i << "/" << i << "\n";
}
这部分代码应该输出..
f 1/1/1 2/2/2 3/3/3 4/4/4
但是出来了..
f 1971327331/1971327331/1971327331 0x28f5a81971327332 / 1971327332/1971327332 0x28f5a81971327333 / 1971327333/1971327333 0x28f5a81971327334 / 1971327334/1971327334
我搜索了几个小时但仍无法找到解决办法。
编辑:感谢LihO,我的问题是我将TEX_TOP fstream对象放在每一行的开头,假设我正在重新声明它,并且int必须从0开始才能计数。 i = 0;
if(!strcmp(line , "TEX:TOP"))
{
i++;
TEX_TOP << i << "/" << i << "/" << i << " ";
i++;
TEX_TOP << i << "/" << i << "/" << i << " ";
i++;
TEX_TOP << i << "/" << i << "/" << i << " ";
i++;
TEX_TOP << i << "/" << i << "/" << i << "\n";
}
答案 0 :(得分:2)
我假设您使用的是未初始化的变量,导致未定义的行为:
int i;
尝试在开始递增其值之前明确地将其初始化为零:
int i = 0;
另请注意,您将fstream
对象本身传递给operator<<
:
TEX_TOP << TEX_TOP << i << "/" << i << "/" << i << " ";
会导致文件中出现其他不需要的内容。只需将其更改为:
TEX_TOP << i << "/" << i << "/" << i << " ";