Reading Text File Weirdness


kouky

Member
Joined
Sep 3, 2006
Messages
185
Location
London
Website
www.caou.org
Hello all,
on my program, I use:

lettre=fgetc(textFile);

to read data from a text file.
It's working perfectly xhen I compille for windows XP; but the same code makes my gp2x to freeze :/

Is there some extra care to do when reading text data on the gp2x ?
 
Hi,

That shouldn't be causing a problem.
Please could you post more of you code as the problem may be caused by another part.
 
I'm sure what GP2x doesn't like is in those lines, because hen I comment them, the software runs ok.
(It's functions to read xml nodes)

Code:
void finLigne()
{
unsigned char w=fgetc(XMLFile);	
}

string lireMot(string motCible)
{
	string mot,lettre,sortie;
	mot="";
	unsigned char i;
	
	for( i=1; i<44; i++ )
	{
		lettre=fgetc(XMLFile);
		mot=mot+lettre;
		
		//s1.substr( 1, 3 );
		if (mot.size()>=motCible.size())
		{
			if (mot.substr( mot.size()-motCible.size(), motCible.size() )==motCible)
			{
			   goto finMot;
			}
			if (mot.substr( mot.size()-8, 8 )=="</decks>")
			{
			   sortie="</decks>";
			   cout<<"fin de TOUT";
			   goto finMot;
			}
		}
	}
	finMot:
	i=1;
	//cout <<mot<<endl;	 
	//cout <<i*1<<endl;
	return(sortie);
}

string lireNoeud(string noeud)
{
	string entree="<"+noeud+">";
	string sortie="</"+noeud+">";
	string mot,lettre;
	string contenu;
	mot="";
	unsigned char i;
	//lire entree
	for( i=1; i<44; i++ )
	{
		lettre=fgetc(XMLFile);
		mot=mot+lettre;
		
		if (mot.size()>=entree.size())
		{
			if (mot.substr( mot.size()-entree.size(), entree.size() )==entree)
			{goto finEntree;}
		}
		if (mot.size()>=8)
		{
			if (mot.substr( mot.size()-8, 8 )=="</decks>")
			{
			   sortie="</decks>";
			   //cout<<"fin de TOUT";
			   goto finNoeud;
			}
		}
		if (mot.size()>=10)
		{
			if (mot.substr( mot.size()-10, 10 )=="</oneDeck>")
			{
			   sortie="</oneDeck>";
			   //cout<<"fin d un deck";
			   goto finNoeud;
			}
		}
	}
	finEntree:
	mot="";
	//trouver sortie
	for( i=1; i<44; i++ )
	{
		lettre=fgetc(XMLFile);
		mot=mot+lettre;
		if (mot.size()>=sortie.size())
		{
			if (mot.substr( mot.size()-sortie.size(), sortie.size() )==sortie)
			{goto finSortie;}
		}
	}
	finSortie:
	//deduire contenu
	contenu=mot.substr( 0, mot.size()-sortie.size() );
	cout <<" >"+contenu+"<"<<endl;
	return(contenu);
	finNoeud:
	//fin d'un noeud
	contenu=sortie;
	//cout <<"FIN x"+contenu+"x"<<endl;
	return(contenu);
}

void lireXML()
{
	cout <<"start"<<endl;
	unsigned char i,j;
	string ligne,mot;
	ligne="";
	XMLFile = fopen("k2x/decks.txt","r");
	for( j=1; j<44; j++ )
	{
		ligne = ligne+to_string(fgetc(XMLFile));
	}
	finLigne();
	mot=lireMot("<decks>");
	
	//bool finNoeud;
	
	for (i=0;i<19;i++)
	{
		mot=lireMot("<oneDeck>");
		if (mot=="</decks>"){cout <<"fin decks"<<endl;break;}
		cout <<" folder:";
		mot=lireNoeud("folder");
		//if (mot=="</oneDeck>"){cout <<"fin deck"<<endl;break;}
		if (mot=="</decks>"){cout <<"fin decks"<<endl;break;}
		deckNames[i]=mot;
		for (j=0;j<9;j++)
		{
			mot=lireNoeud("oneVideo");
			if (mot=="</oneDeck>"){cout <<"fin deck"<<endl;break;}
			if (mot=="</decks>"){cout <<"fin decks"<<endl;break;}
			videoNames[i*10+j]=mot;
		}
		//j=lireMot("</oneDeck>");
		if (mot=="</decks>"){cout <<"fin decks"<<endl;break;}
	}
	maxDeck=i;
	fclose (XMLFile);
	
	cout <<"stop"<<endl;
	cout <<"decks: ";
	cout<<(1*maxDeck)<<endl;/**/
}
 
Two quick comments:
- mixing C and C++ IO is probably not a good idea
- after opening the file check it's been indeed correctly opened (XMLFile should not be NULL).
 
fgetc can return EOF so you might want to check for that as well.
Code:
	   fgetc() reads the next character from  stream  and  returns  it  as  an
	   unsigned char cast to an int, or EOF on end of file or error.
 
Maybe this is a stupid question, but when you load the file, is it the correct filename? I believe its case sensitive...
 
I assume your file handle is global or you would not even get this to compile and I would check to see if XMLFile comes back as NULL to see if the open was a success or not.
 
Thanks for all your help!
By commenting some parts of the code; I've concluded that the error is somewhere here:
Code:
string lireMot(string motCible)
{
	string mot,lettre,sortie;
	mot="";
	unsigned char i;
	
	for( i=1; i<44; i++ )
	{
		lettre=fgetc(XMLFile);
		mot=mot+lettre;
		
		//s1.substr( 1, 3 );
		if (mot.size()>=motCible.size())
		{
			if (mot.substr( mot.size()-motCible.size(), motCible.size() )==motCible)
			{
			   goto finMot;
			}
			if (mot.substr( mot.size()-8, 8 )=="</decks>")
			{
			   sortie="</decks>";
			   cout<<"fin de TOUT";
			   goto finMot;
			}
		}
	}
	finMot:
	i=1;
	//cout <<mot<<endl;//cout <<i*1<<endl;
	return(sortie);
}

I will now add a "end of file" error check
 
I've found the problem:
when you do:
if (mot.substr( mot.size()-8, 8 )=="</decks>")

when mot.size() is less than 8, then gp2x freeze.
 
That makes sense, it looks like the same problem my Quake MAP utility was having when looking for object tags...

Its just that my utility ONLY ran in windows and had this problem. This problem exists in the GP2x and NOT windows... thats weird, still.
 
Offtopic, but why are you using goto? When a break would be fine?
Code:
string lireMot(string motCible)
{
	string mot,lettre,sortie;
	mot="";
	unsigned char i;
	
	for( i=1; i<44; i++ )
	{
		lettre=fgetc(XMLFile);
		mot=mot+lettre;
		
		//s1.substr( 1, 3 );
		if (mot.size()>=motCible.size())
		{
			if (mot.substr( mot.size()-motCible.size(), motCible.size() )==motCible)
			{
			   break;
			}
			if (mot.substr( mot.size()-8, 8 )=="</decks>")
			{
			   sortie="</decks>";
			   cout<<"fin de TOUT";
			   break;
			}
		}
	}
	i=1;
	//cout <<mot<<endl;//cout <<i*1<<endl;
	return(sortie);
}
 
Back
Top