Cannot compare TSystem::HostName()

Hi All,

I am doing some stupid mistake here but I cannot figure it out:

in my root code I do:

std::cout << gSystem->HostName() << std::endl;
if (gSystem->HostName() == “pcatlas264.cern.ch”)
{
std::cout << “test” << std::endl;
}

on runtime it says:
pcatlas264.cern.ch

and thats it. How come it does the == wrong? I also tried
strcmp(gSystem->HostName(), “pcatlas264.cern.ch”)
also does not go into the if …

How can I compare the result of the hostname method to a string??

Thanks
Andi

Hi,

have a look at the definition of the return code of strcmp - it returns 0 for equality. You could also use TString hostname(gSystem->GetHostname()), and then compare the TString using its operator==. The way you invoke the operator== C++ compares the memory addresses of the const char* returned by GetHostName and your local const char* “pcatlas264.cern.ch” - which will obviously always fail. So const char* hostname=gSystem->HostName(); if (hostname && !strcmp(hostname, "pcatlas264.cern.ch"))will work for you.
Axel.

Hi,

thanks it works now!

Cheers
Andi