Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/11/2011, 05:28
MiGoBi
 
Fecha de Ingreso: mayo-2011
Mensajes: 60
Antigüedad: 13 años
Puntos: 1
Problema comparando strings, ¿por qué no me devuelve 0?

Hola, estoy leyendo un fichero y cuando llego a la 2ª linea comparo lo leído con un string declarado por mí. El problema que tengo es que siempre me devuelve 1, y según la documentación que he leído debería devolver 0 para ser una comparación correcta. Para corroborarlo me he creado unos strings y los he comparado y sí es como dice la documentación (como es lógico). Entonces he pensado que el problema puede ser del fichero que leo (a pesar de que los strings son idénticos).¿Alguna idea de por qué me está sucediendo esto? Gracias de antemano:

Código:
int firstPosc=0, frequency, strlLengthPuntoComa, cont=0;
double valueRead;
string strTextLine, weights="<Weights>", tecla;

//Para la lectura de cada linea del fichero
char textLine[250];
//Para la lectura del fichero
char *c, *puntoYComa;

//Ignoramos la 1ª linea
c = fgets(textLine, 250, dataFile);

//La segunda linea debe ser igual a: <Weights>
c = fgets(textLine, 250, dataFile);
strTextLine=string(textLine);

    
cout << "strTextLine: " << strTextLine << endl;
cout << "weights: " << weights << endl;
cout << "strTextLine.compare(weights): " << strTextLine.compare(weights) << " ,pulsa algo para continuar(g): " << endl; --> ESTA COMPARACION ME DEVUELVE 1


//    string t1str="A", t2str="A", t3str="B";
//    cout << "t1str: " << t1str << " t2str: " << t2str << " t3str: " << t3str << endl;
//    cout << "t1str.compare(t1str): " << t1str.compare(t1str) << endl;
//    cout << "t2str.compare(t2str): " << t2str.compare(t2str) << endl;
//    cout << "t1str.compare(t2str): " << t1str.compare(t2str) << endl;
//    cout << "t3str.compare(t2str): " << t3str.compare(t2str) << " ,pulsa algo para continuar(g): " << endl;  
//	cin >> tecla; // --> ESTAS COMPARACIONES ME DEVUELVE LOS VALORES CORRECTOS

    if(strTextLine.compare(weights)==0){ //SIEMPRE ME DEVUELVE 1 LA COMPARACIÓN
    	//La tercera contiene los datos y la 4ª que sería el cierre de tag nos da igual
    	c = fgets(textLine, 250, dataFile);
    	strTextLine=c;
    	frequency=count(strTextLine.begin(), strTextLine.end(), ';');
    	if(frequency==arrayDoubleSize){
    		//Pedimos memoria
		(*arrayDoubles_)=new double[arrayDoubleSize];
		puntoYComa=strchr(textLine,';');
		while(puntoYComa!=NULL){
			strlLengthPuntoComa=puntoYComa-textLine+1;
			valueRead=XMLReaderObtainFloatValue(firstPosc, strlLengthPuntoComa, textLine);
			(*arrayDoubles_)[cont]=valueRead;
			cout << valueRead << " :: " << endl;
			cont++;
			firstPosc=puntoYComa-textLine+1;
			puntoYComa=strchr(puntoYComa+1,';');
		}
		//error = 0 --> OK
    	}
    	else{
    		(*error_)=-2;
    		// Debe haber el mismo número de ; que de valores a leer
    	}
    }
    else{
    	(*error_)=-1;
      	//El fichero está mal formado, falta la apertura de tag (no compruebo el cierre)
    }
}
Y esto es lo que me aparece por consola:
Código:
Path del fichero XML con la configuracion: 
C:\temp\pesos.xml
strTextLine: <Weights>

weights: <Weights>
strTextLine.compare(weights): 1 ,pulsa algo para continuar(g):