checking number of variables in a file?

Started by
6 comments, last by iMalc 18 years, 3 months ago
Hello, I'm wondering if it's possible to check how many variables there is in a file since I'm trying to load a .raw model for my opengl project and I need to know how many faces there is.
Advertisement
Quote:Original post by The Imp
Hello,
I'm wondering if it's possible to check how many variables there is in a file since I'm trying to load a .raw model for my opengl project and I need to know how many faces there is.


You can check the length of a file if objects are the same size, or you can include an information block in the file that has a count of the objects / variables.

I don't know about the .raw model format, it might or might not keep a count inside it.
Files don't contain "variables"; they contain data. Any file can be looked at as just a sequence of bytes; the file size (length) is simply the total number of bytes. However, just reading in a file into a single chunk of memory (like a char[] in C++ or byte[] in Java) is rarely useful.

The file format (this is documentation, not something embedded in the file) explains the *meaning* of the data, and how it is logically organized - including how many "units" of whatever type there are.

It is common, whenever a file is allowed (according to the format) to contain a non-constant (i.e. differing between files of that type) number of some "thing", to include as well a count of the number of "things" that are represented. However, there are a few different ways to represent the count itself, and it isn't always included (sometimes it is considered to be implicit, because you can deduce that x-many bytes of the file represent "things", and every "thing" is represented in the same number y of bytes - it is not always possible or feasible to represent things like that, though).

So you need to know the format of the data in question.



BUT - depending on your implementation language, you should be able to load a set of data of the same type from a file without knowing in advance how many items there are. In C++ this is most effectively done with one of the standard library containers - typically std::vector.
A raw file goes something like this:

[VERTEX1 X Y Z] [VERTEX2 X Y Z] [VERTEX3 X Y Z] etc... Each line is one triangle.

Example

 0.468750  0.242187  0.757813  0.437500  0.164062  0.765625  0.500000  0.093750  0.687500 0.500000  0.093750  0.687500  0.562500  0.242187  0.671875  0.468750  0.242187  0.757813-0.500000  0.093750  0.687500 -0.437500  0.164062  0.765625 -0.468750  0.242187  0.757813-0.468750  0.242187  0.757813 -0.562500  0.242187  0.671875 -0.500000  0.093750  0.687500 0.562500  0.242187  0.671875  0.500000  0.093750  0.687500  0.546875  0.054687  0.578125 0.546875  0.054687  0.578125  0.625000  0.242187  0.562500  0.562500  0.242187  0.671875-0.546875  0.054687  0.578125 -0.500000  0.093750  0.687500 -0.562500  0.242187  0.671875-0.562500  0.242187  0.671875 -0.625000  0.242187  0.562500 -0.546875  0.054687  0.578125 0.500000  0.093750  0.687500  0.351563  0.031250  0.718750  0.351563 -0.023438  0.617188 0.351563 -0.023438  0.617188  0.546875  0.054687  0.578125  0.500000  0.093750  0.687500-0.351563 -0.023438  0.617188 -0.351563  0.031250  0.718750 -0.500000  0.093750  0.687500-0.500000  0.093750  0.687500 -0.546875  0.054687  0.578125 -0.351563 -0.023438  0.617188 0.437500  0.164062  0.765625  0.351563  0.132812  0.781250  0.351563  0.031250  0.718750


To store the data I use this method

VERTEX v[number of vertices]

fscanf(pFile, "%f %f %f", &v[n].x, &v[n].y, &v[n].z);

This is why I need to know how many vertices there is.
fseek(fp, 0, SEEK_END);
int nvertecies = ftell(fp)/(sizeof(float)*3);


That should do the trick as long as the file is opened in binary mode, and assuming there is no file header, etc and only raw vertex data. Otherwise it would be:

fseek(fp, 0, SEEK_END);
int nvertecies = (ftell(fp)-headeroffset)/(sizeof(float)*3);

Although if there is a header it should tell you how many there are anyway, rendering this useless....
There is evidently no header, and the numbers are apparently given in text (human-readable) format.

If those are *spaces* in between the numbers, and there are *always* 9 numbers (one triangle) per line (or if there aren't any returns at all and you're just wrapping it for display), then you may be in luck: count the total file length, calculate the expected line length (I get 90 bytes including one for the return - but to count file length I think you will need to open in binary mode, and then you will need to know how the line endings are encoded, as CRLF or just CR), and then you know how many triangles there are.

Just for the hell of it - in C++, using a std::vector you can avoid worrying about the count like this:

std::vector<VERTEX> vertices;VERTEX v;while (myfile >> v.x >> v.y >> v.z) {  vertices.push_back(v);}
I'm pretty sure the file is stored as text, so sizeof(float) isn't going to be of any use.
However, you'll notice that every float in the file takes up the same number of characters. If the number is negative it has a minus sign, and it positive, a space is put there instead.
I count exactly 10 characters per float, so I would declare a constant to equal ten and use that. I would also assume it's not a unicode file?
fseek(fp, 0, SEEK_END);int nvertecies = (ftell(fp)-headeroffset)/(BYTES_PER_FLOAT);
One last thing though, a carriage return at the end of each line might muck that up, so you may instead have to work out the number of characters per line and use that instead.
Even if what you end up with is only an estimate, then as long as you always only ever overestimate, then everything should be okay.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
You might also consider only reading in unique points, and storing indexes to them in the triangles. I.e. as an indexed buffer format, which may reduce memory usage.
Though if you do that you'll want to use a hash table for duplicate detection.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement