Archived

This topic is now archived and is closed to further replies.

Zeus_666

C++ - Getting Vertex data from text file into array

Recommended Posts

Hi, I have a txt file full of vertex data fort he polygons i want to draw but the problem i am having is reading the numbers both positive and negitive seperatored by space, 9 -4 7 -1 -4 0 into the array without putting in the spaces and the ''-'' sysmbo on its own. I anyone could help, i would be most grateful. Thanks, Paul

Share this post


Link to post
Share on other sites
I'm not sure why you're not doing binary, or if there are any newlines ("\n") but here is a possible hack.

fscanf("%f %f %f", &Point.x, &Point.y, &Point.z);

I don't know your file format so it's kinda hard to help any more than that.
Good Luck,
~S'Greth

Edited by - SGreth on June 13, 2001 8:06:48 AM

Share this post


Link to post
Share on other sites
quote:
Original post by Zeus_666

Hi,
I have a txt file full of vertex data fort he polygons i want to draw but the problem i am having is reading the numbers both positive and negitive seperatored by space,

9 -4 7 -1 -4 0

into the array without putting in the spaces and the ''-'' sysmbo on its own. I anyone could help, i would be most grateful.

Thanks, Paul

To read an integer from a file, you can use the standard extractor operator (>> on an ifstream:

ifstream file;
int i;
file >> i;

It will read a negative number (-15) as easily as a postive number (15).

To do a loop to read ints, do the following:

while (file >> i)
{
// do whatever you like with i
}

This will read ints from file until an error occurs (i.e. input that cannot be converted to an int), or end-of-file is reached.

Integers in the file have to be separated by whitespace (space, tab, newline).

HTH



Some useful C++ links:
Free multiplatform ANSI C++ Standard Library implementation
Visual C++ STL fixes
Visual C++ 6.0 noncompliance issues
C++ FAQ Lite

Share this post


Link to post
Share on other sites
But not just int''s, the >> operator can read any standard data type from a file. Since i''m sure you''re using floats or doubles, do something of the following

#include
#include

#define MAX_SIZE 250000
...

ifstream fin;
float n[MAX_SIZE];

fin.open("model_file");
// dont forget to check for errors

fin >> n[0];
for(int i=0; !fin.eof() && i < MAX_SIZE; i++)
fin >> n;

fin.close();

.....
this will read all the data from the file
it keeps track of what''s negative and what''s positive also. As long as they are seperated by spaces or new lines. However, i would suggest you do it in binary like the person who suggested it earlier. If you need help, I have a decent binary file format that makes it easy to use features like index vertex arrays, etc.

Share this post


Link to post
Share on other sites
You can indeed use the stream extractor and inserter operators for any of the built in types, as Hairu pointed out (thanks).

However, (and this is more to Hairu), I''d like to make some small comments on the code you posted (just to clarify, not to mean any disrespect):

- iostream.h or fstream.h are old, pre standard-C++ headers, and their use is deprecated. All C++ Library headers have no .h. So, #include <iostream>. This also means that the C++ classes and types are in namespace std, so you have to use ''using namespace std'', or specify the scope directly (std::ifstream, for example).
- in your for loop, you only check for eof(). But, there may be other errors, like input that cannot be converted. Better is to use while(f >> n) { ... }
- last but not least: use vector instead of c-style arrays. C-style arrays are error prone, and can be difficult to use.

HTH



Some useful C++ links:
Free multiplatform ANSI C++ Standard Library implementation
Visual C++ STL fixes
Visual C++ 6.0 noncompliance issues
C++ FAQ Lite

Share this post


Link to post
Share on other sites
Ah, I did not know that you could leave the .h off of those headers. I only did that for the c++ string class. I was wondering though, what exactly did you mean by use vectors instead of arrays? You mean to create a vector structure yourself, or does c/c++ have built in vectors?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi,
Thanks for the help, a few have mentioned doing the file in binary format, how would i go about this?

Thanks, Paul

Share this post


Link to post
Share on other sites
Using:

while( !file.eof() )
{
//Do stuff here
}

Will avoid having to use an empty while loop. In the while loop you probably will be storing the stuff in the vector.

"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

Share this post


Link to post
Share on other sites
quote:
Original post by EbonySeraph

Using:

while( !file.eof() )
{
//Do stuff here
}

Will avoid having to use an empty while loop. In the while loop you probably will be storing the stuff in the vector.


Actually, it''s better not to use file.eof() in that way. Usually, people write things like
  
ifstream file("blabla");
while (!file.eof())
{
// ... read from file ...

// ... do whatever with data read ...

}

with the intention of reading data until the end of a file. There is a fundamental flaw in this approach: the eof file flag is only set after attempting to read past the end of a file. Consider this for an input steam mapped to keyboard input. It''s impossible to predict whether or not a character the user typed will be the last one! So the loop above will not reliably read the file.

Better is to use
  
while ( file >> i )
{
// ... do whatever with data read ...

}

in case of formatted input, or
  
while ( file.read(buf, n) )
{
// ... do whatever with data read ...

}

in case of unformatted input. Or you can use something else than a while loop, of course!

HTH



Some useful C++ links:
Free multiplatform ANSI C++ Standard Library implementation
Visual C++ STL fixes
Visual C++ 6.0 noncompliance issues
C++ FAQ Lite

Share this post


Link to post
Share on other sites