开发者

Flexible string handling in Visual Studio 2008 C++

开发者 https://www.devze.com 2022-12-30 18:22 出处:网络
I\'m slowly starting to get the hang of the _T stuff in Vi开发者_运维技巧sual 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

I'm slowly starting to get the hang of the _T stuff in Vi开发者_运维技巧sual 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);
} 


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消