views:

133

answers:

2

I'm slowly starting to get the hang of the _T stuff in Visual Studio 2008 c++, but a few things still elude me. I can see the benefit of the flexibility, but if I can't get the basics soon, I think I'll go back to the standard way of doing this - much less confusing.

The idea with the code below is that it scans the parameters for -d and then stores the text that follows that in the string variable fileDir. It also ignores any other parameters.

Any help is appreciated.

//Console application
Parameters::Parameters(int argc, _TCHAR* argv[]) 
{ 
    _Tstring fileDir;  // Is there some kind of _t variable to use here for a string?

    for (int i = 0; i < argc; i = i + 1)
    {           
        if (_tccmp(argv[i], _T("-d")) == 0)  // this appeared to accept anything starting with -
        {           
            i = i + 1;
            fileDir = argv[i]
        }
    }   

    _tprintf("Parameter value found: %s\n", fileDir);
} 
+1  A: 

You can use _tstring to represent and std::string with a TCHAR parameter. Also, the name of the function is _tcscmp, not _tccmp so I don't see how that code snippet could even compile?

To be honest, I wouldn't bother with any of that, though. The whole TCHAR stuff was useful back when people were writing code that was to be portable between Windows 9X (which was Ansi internally) and Windows NT (which was/is Unicode internally). These days, there's very little benefit to using the macros.

If you want to do anything, you can switch to wchar_t entirely. That means prefixing string literals with "L", as in L"Some wide-char string" and using the 'w' versions of functions (e.g. std::wstring, wcscmp, etc). But even that might be considered overkill for many scenarios.

Dean Harding
I would argue about TCHAR, people may want to wrote cross-platform apps.
Sorin Sbarnea
TCHAR is not portable to any other platform anyway (you'd have to re-write all the macros yourself on the other platform). If you want to be cross-platform, I would stick with UTF-8 encoding internally. Or use some cross-platform library (like Qt, etc)
Dean Harding
A: 

If you want to copy a string value and you're using MFC or ATL, your _Tstring can be CString.

If you want to copy a pointer value, your _Tstring can be LPTCSTR. (This works whether or not you're using MFC or ATL.)

As codeka said, your _tccmp needs to be _tcscmp. An alternative, if you're using MFC or ATL, is CString's Compare method.

You didn't show how the caller delivers parameter argv to your constructor. If argv came from your _tmain function, make sure the parameters to _tmain are correct.

Windows programmer