tags:

views:

62

answers:

2

Hello everyone

I have this method which receives a path through a TCHAR szFileName[] variable, which contains something like C:\app\...\Failed\

I'd like to sort through it so I can verify if the name of the last folder on that path is in fact, "Failed"

I thought that using something like this would work:

std::wstring Path = szFileName;

string dirpath2;
dirpath2 = Path.substr(0,5); 

But I get the following error:

Error 6 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)

Needless to say, I'm very new to C++, and I've been looking for an answer for a while now, but I haven't had any luck, so any help would be appreciated :)

+2  A: 

Either you’re consistently using wstring (the wide character variant) or string (the “normal” variant).

Since you’re getting a TCHAR (which can be either wchar_t or char, depending on compiler flags), the appropriate type to use would be a tstring, but that doesn’t exist. However, you can define a typedef for it:

typedef std::basic_string<TCHAR> tstring;

Now you can consistently use the same string type, tstring, for all your operations.

Konrad Rudolph
I'm sorry but I don't fully understand what goes on there.am I supposed to do something like: std::tstring Path = szFileName;'cause if so, then I get the following error:'tstring' : is not a member of 'std'
hikizume
You’re defining `tstring` yourself, and it’s not in the namespace `std` so when you use it, don’t prefix it with `std::`.
Konrad Rudolph
it seems to work but why do I get an error "initializing' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::basic_string<_Elem,_Traits,_Ax>'" when I try to use substr to store a part of the initial string on another string?
hikizume
You are still using different string types! You need to use `tstring` consistently for both strings!
Konrad Rudolph
my bad! It's working now. Thanx a lot!
hikizume
+2  A: 

dirpath2 has to be a std::wstring as well. There are ways to convert between the two, but they involve changing the character encoding and that seems like more than you're asking for.

I like to simply not use TCHAR. Today, there is rarely is there a need to enable or disable the UNICODE macros and create both an ASCII and a Unicode version of a program. Just always use wstring, wchar_t, and the Windows API functions that end in 'W'.

If you're working on something where you don't have control over the above, Konrad's typedef answer is more practical than mine.

Steve M