I need to read the contents of a text file and store the values to variables. Consider this simple file:
2
-0.5 -0.5 0.0
0.5 -0.5 0.0
When specifying its filename (so without using resources), I proceed like this with fscanf_s
:
FILE *pFile;
fopen_s(&pFile, filename.c_str(), "r");
fscanf_s(pFile, "%d", &nPoints);
points = new Vec3[nPoints];
for (int n = 0; n < nPoints; ++n)
fscanf_s(pFile, "%lf %lf %lf", &points[n][0], &points[n][1], &points[n][2]);
fclose(pFile);
and the data is saved to two vectors, each having three values.
Now, I would like to do the same but with a file that is included as a user-defined resource. First, I follow this example to load the data into a buffer. The problem is that I don't know what to do with this buffer to retreive the data and save it in a similar way. I have tried using the sscanf_s
function:
sscanf_s(buffer, "%d", &nPoints);
points = new Vec3[nPoints];
for (int n = 0; n < nPoints; ++n) {
sscanf_s(buffer, "%lf %lf %lf", &points[n][0], &points[n][1], &points[n][2]);
}
but it doesn't seem to work like I expected. The number of points is read correctly into the nPoints
variable, but both vectors end up with the values 2, -0.5, -0.5.
How can I save the values from the buffer to my Vec3
s? Or is there a simpler alternative that I should consider?