سؤال

Forgive my C++ naivete. I need to compare two characters in the parsing of a .OBJ 3D object file. On each line iteration, the strcmp calls I make are never returning true. I have a feeling this is because I'm not up on my char* vs char knowledge. Anybody see what I'm doing wrong here?

//Variables
char* type = new char[1];

float v1;
float v2;
float v3;

//INSIDE the while loop that parses each line of the file
getline(myfile, line);
sscanf(line.c_str(),"%c %f %f %f", type, &v1, &v2, &v3);
if(strcmp(type,"f") == 0){
    faces++;
}
if(strcmp(type,"v") == 0){
vertices++;
}
هل كانت مفيدة؟

المحلول

"strcmp" compares null terminated strings, but, you have defined "type" as a single character not an array of characters terminated by x'00' as expected by strcmp.

A simple if (type == 'f') char comparison should get you the correct result.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top