Got not get a correct value from std::string compare

Hi,

I could not understand following problem, please enlighten me:

I am running a program in FC9 (gcc version 4.3.0 20080428 (Red Hat 4.3.0-8) (GCC)), it is running fine except that the std::string compare does not work. It got a wrong return of std::string compare value, but works to C compare. Please see following a debug printf statement:

printf("path = .%s. CPP compare = %d, C compare = %d\n", path.c_str(), path.compare("/"), strcmp(path.c_str(), "/"));

It printed out following result:

path = ./. CPP compare = 1, C compare = 0

I don't think it is a problem of the std::string compare at all, I suspect it might be some data crapsed in my program, but the program is running perfect if I use the C string compare function. I could not figure out what was happen. Any thoughts and comments please?

Thank you.

Kind Regards,

Mayer



Mayer,

Could you post youre code? I wrote this with the following output:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstdio>

using namespace std;

int main (int argc, char * const argv[]) {
    int temp;
	string path = "/";
	printf("path = .%s. CPP compare = %d, C compare = %d\n", path.c_str(), path.compare("/"), strcmp(path.c_str(),"/"));
	cin >> temp;
    return 0;
}


path = ./. CPP compare = 0, C compare = 0


From what I understand you are trying to do, it appears it should work fine, just as you have it written, so my suspicion is that there is something wrong with your value for path.
Last edited on
Topic archived. No new replies allowed.