我正在浏览我朋友的obj加载代码.他用C++完成了它; 代码是:
bool LoadObj(char* given){
char tempText[60];
std::ifstream OB(given);
OB.seekg(0,OB.end); int length = OB.tellg() ; OB.seekg(0,OB.beg);
char* STREAM = new char[length];
OB.read(STREAM,length);
OB.close();
char *t,dump[20];
int Number_Of_Vertices,Number_Of_faces;
t = strstr(STREAM,"vertices");
sscanf(&STREAM[&t[0]-&STREAM[0]-10],"%s # %i vertices",&dump,&Number_Of_Vertices);
printf("\nthere are %i vertices",Number_Of_Vertices);
t = strstr(STREAM,"faces");
sscanf(&STREAM[&t[0]-&STREAM[0]-10],"%s # %i faces",&dump,&Number_Of_faces);
printf("\nthere are %i faces",Number_Of_faces);
......
Run Code Online (Sandbox Code Playgroud)
当我通过加载obj文件测试它时,它正确打开.但是我不明白第一个论点sscanf(),即:&STREAM[&t[0]-&STREAM[0]-10].请解释它是如何工作的?
t = strstr(STREAM,"vertices");
Run Code Online (Sandbox Code Playgroud)
t将指向子串的位置,在"vertices"里面STREAM.
&t[0]是第一个字符的地址"vertices".相当于t自己.这同样适用&STREAM[0],它是第一个字符的地址STREAM.减去它们可以得到起始索引t.
所以你的朋友想要阅读STREAM,之前 开始十个字符t.
通过一个简单的方法可以更清楚地表达所有这一切t - 10.