Sign in to follow this  
paulshady

ifstream help....parsing a .x file

Recommended Posts

		int totalfaces;
		int totalindices;
		inFile >> totalfaces;
		totalindices = totalfaces * 3;
		data.SetIndexCountForObject( objectcount, totalindices );
		inFile >> str; // reads in a semicolon
		for ( int indexcount = 0; indexcount < totalindices; indexcount+=3 )
		{
			char ch;
			int index, temp;
			inFile >> temp; // 3 indices per face, reads in the "3"
			inFile >> ch; // reads in a semicolon
            inFile >> index;
			data.SetIndex( objectcount, indexcount, index );
			inFile >> ch; // reads in a comma
            inFile >> index;
			data.SetIndex( objectcount, indexcount + 1, index );
			inFile >> ch; // reads in comma
            inFile >> index;
			data.SetIndex( objectcount, indexcount + 2, index );
			inFile >> ch; // reads in a semicolon
			inFile >> ch; // reads in a comma
		}


this small cpp chunk is reading the indices from a .x file. this part, specifically:
   12;
   3;0,3,2;,
   3;3,0,1;,
   3;4,7,5;,
   3;7,4,6;,
   3;8,9,10;,
   3;9,8,11;,
   3;1,7,12;,
   3;7,1,13;,
   3;14,15,16;,
   3;15,14,17;,
   3;18,19,0;,
   3;19,18,6;;

those are the indices in an x file...anyways, im having a problem at the first inFile >> index statement. when index is declared it is some arbitrary number, lets say 1241528, then the inFile statement is hit and index does not change. This code worked on my previous machine but not my new one. Any advice? ideas? could the functionality/output of the streams differ on different implementations? any help would be appreciated.

Share this post


Link to post
Share on other sites
The comma is being interpreted as part of the integer when it's trying to read it in. This is a locale problem I believe. Look into locales and also you can use the stream state to see if an operation will fail. inFile.fail() will return true if it couldn't read the integer. If you're lazy though you could just do a find and replace to replace all of the commas with semicolons.

Here's how you can remove the ',' grouping (which is the default, but maybe not on your old system) when reading in integers:


class NoSep_Punct : public std::numpunct<char> {
public:
explicit NoSep_Punct(size_t r=0) : std::numpunct<char>(r) { }
protected:
char do_thousands_sep() const { return NULL; }

// Then later after you define your ifstream

locale loc(locale(), new NoSep_Punct);
inFile.imbue(loc);
};



[Edited by - Absolution on October 5, 2006 3:22:05 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this