Troubles with NeHe's Lesson 10
This is the code to import a list of vertices that will become a small 3d world. I can't move past this lesson because I dont understand this code.
void SetupWorld()
{
float x, y, z, u, v;
int numtriangles;
FILE *filein;
char oneline[255];
filein = fopen("data/world.txt", "rt"); // File To Load World Data From
readstr(filein,oneline);
sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles);
sector1.triangle = new TRIANGLE[numtriangles];
sector1.numtriangles = numtriangles;
for (int loop = 0; loop < numtriangles; loop++)
{
for (int vert = 0; vert < 3; vert++)
{
readstr(filein,oneline);
sscanf(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v);
sector1.triangle[loop].vertex[vert].x = x;
sector1.triangle[loop].vertex[vert].y = y;
sector1.triangle[loop].vertex[vert].z = z;
sector1.triangle[loop].vertex[vert].u = u;
sector1.triangle[loop].vertex[vert].v = v;
}
}
fclose(filein);
return;
}
The "sscanf" function gets me. I know its reading the string stores in "oneline" but after that who knows. "NUMPOLLIES" is a word at the top of a text file. Just after that word is the number of polygons the map will contain. I know there are a lot of things you can do with the syntax here, all those %d or %f or whatever, but I cant find a list online that shows thier meanings, and the Microsoft devnet isnt helping either. Any help is appreciated.
sscanf basically reads in a string from a file. Basics:
%d - integer
%f - float
%s - string
The sscanf in this instance, grabs the variables needed to render a triangle. It loops three times for each vertex (a triangle is 3 vertexes), which is inside a loop to grab each triangle.
%d - integer
%f - float
%s - string
The sscanf in this instance, grabs the variables needed to render a triangle. It loops three times for each vertex (a triangle is 3 vertexes), which is inside a loop to grab each triangle.
%f is a floating point number
%d is an integer
sscanf scans a string for numbers using the format specified. So...
that scans the string 'online' for "NUMPOLLIES " then it looks for an integer which would then be stored in the variable numtriangles. The \n just means it is the end of the line.
Those lines read the next line in the text file and stores it in the oneline variable. Then it scans the oneline variable for 5 floating point numbers separated by spaces and then stores them in the variables x, y, z, u and v.
Its kinda hard to explain, but i hope that helps.
Kazade.
%d is an integer
sscanf scans a string for numbers using the format specified. So...
sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles);
that scans the string 'online' for "NUMPOLLIES " then it looks for an integer which would then be stored in the variable numtriangles. The \n just means it is the end of the line.
readstr(filein,oneline);sscanf(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v);
Those lines read the next line in the text file and stores it in the oneline variable. Then it scans the oneline variable for 5 floating point numbers separated by spaces and then stores them in the variables x, y, z, u and v.
Its kinda hard to explain, but i hope that helps.
Kazade.
Member of the NeHe team.
I take it you started with real C++ and have never seen the printf() family of functions before, eh?
No biggie. Look up printf() or scanf() and it should give you a list of the control codes for input/output. As a quick rundown, the sscanf() function scans a string for a specific input.
The string to scan is the first parameter: online in this case.
The second parameter is the format string, telling it how the input should look. "NUMPOLLIES %d\n" means that it is too look specifically for the word NUMPOLLIES followed by a space, then the %d stands for an integer number to be retrieved. Other examples are %s for string, and %c for character.
Then after the format string, you have one parameter for each control code in the format string. The sscanf() function will actually put values to the variables since it's an input function. As such, it needs the address of numtriangles to be able to edit the value in it.
The number of parameters here can be arbitrarily long depending on how many codes you've got in the format string.
Hope that clears stuff up. If it doesn't, look up printf()/scanf() or read through a beginner's C tutorial (not C++).
-Auron
No biggie. Look up printf() or scanf() and it should give you a list of the control codes for input/output. As a quick rundown, the sscanf() function scans a string for a specific input.
The string to scan is the first parameter: online in this case.
The second parameter is the format string, telling it how the input should look. "NUMPOLLIES %d\n" means that it is too look specifically for the word NUMPOLLIES followed by a space, then the %d stands for an integer number to be retrieved. Other examples are %s for string, and %c for character.
Then after the format string, you have one parameter for each control code in the format string. The sscanf() function will actually put values to the variables since it's an input function. As such, it needs the address of numtriangles to be able to edit the value in it.
The number of parameters here can be arbitrarily long depending on how many codes you've got in the format string.
Hope that clears stuff up. If it doesn't, look up printf()/scanf() or read through a beginner's C tutorial (not C++).
-Auron
You guys are great.. 3 replies in less than 20 min. Its true I did start with C++. I could write a term paper on pointers.. but memset and printf are like a foreign language to me. I really appreciate it.
I believe you can call the C++ sscanf stringstream. Say you have a string with this data: "15 15"
now with stringstream you first read in the string:
stream << thestring;
Now when you want to extract the 2 numbers you simply do this:
stream >> num1 >> num2;
sample code:
now with stringstream you first read in the string:
stream << thestring;
Now when you want to extract the 2 numbers you simply do this:
stream >> num1 >> num2;
sample code:
#include <iostream>#include <sstream>using namespace std;int main(){ stringstream stream; stream << "15 15"; int num1; int num2; stream >> num1 >> num2; cout << num1 << " " << num2 << endl;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement