views:

77

answers:

3

hello again

I have this variable dirpath2 where I store the deepest directory name of a path:

typedef std::basic_string<TCHAR> tstring;
tstring dirPath = destPath;
tstring dirpath2 = dirPath.substr(destPathLenght - 7,destPathLenght - 1);

I want to be able to compare it it another string, something like:

if ( _tcscmp(dirpath2,failed) == 0 )
{
...       
}

I've tried a lot of things but nothing seems to work. Can anyone tell me how to do this or what am I doing wrong?

Please bear in mind I know almost next to nothing about C++ and this whole thing is driving me insane.

thanx in advance

+6  A: 

std::basic_string<T> have an overloaded operator==, try this:

if (dirpath2 == failed)
{
...
}

Alternatively you could do like this. As std::basic_string<T> doesn't have an implicit conversion operator to const T*, you need to use the c_str member function to convert to const T*:

if ( _tcscmp(dirpath2.c_str(), failed.c_str()) == 0 )
{
...
}
dalle
This one did the trick, thus keeping me from going insane. thanks a lot!
hikizume
+4  A: 

Why are you using _tcscmp with a C++ string? Just use it's builtin equality operator:

if(dirpath2==failed)
{
    // ...
}

Have a look at the provided comparison operators and methods that can be used with the STL strings.

In general, if you use C++ strings you don't need to use the C string functions; however, if you need to pass C++ strings to functions that expect C-strings you can use the c_str() method to get a const C-string with the content of the specified C++ string instance.

By the way, if you know "almost next to nothing about C++", you should really get a C++ book and read it, even if you come from C.

Matteo Italia
A: 

std::basic_string has a == operator. Use the string classs template:

if (dirpath2 == failed)
{
...
}
harper