Вопрос

I created a project in C++, and VS2010 created

int _tmain(int argc, _TCHAR* argv[])
{

string sInput;
string sOutput;
int iMode=0;

if (argc==3)
{
    if (strcmp(argv[0], "-e")==0)
    {
        iMode = 1;
    } 
    else if (strcmp(argv[0], "-d")==0)
    {
        iMode =2;
    }
    sInput=argv[1];
    sOutput=argv[2];
}
}

As you can see I would like to treat the args as string, but I am not sure how to get from _TCHAR* to std::string.

Can somebody help? Thank you very much!

Это было полезно?

Решение

The easiest way for command line arguments is to choose whether you want them to support wide strings or not. If not, it's easy:

int main(int argc, char *argv[])

If you want them, however, things get a little bit more complicated, but not that much. MSVC has some _wmain or whatever variant, but it's non-standard. I prefer standard main signatures. You can do this:

int main() {
    PWSTR cmdLine = GetCommandLineW();

    int argc;
    PWSTR *argv = CommandLineToArgvW(cmdLine, &argc);
}

The whole TCHAR mess is pretty annoying to deal with. It's better to just use wide strings if you want wide string support and use narrow strings if you don't. When dealing with other Windows API functions, I highly recommend everything going in and out of them be a wide string.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top