Search code examples
c++wchar-twstring

Convert char* argv[] to wstring


How can I convert char* argv[] to wstring? I use "/d" to get a string path from argv and insert it as parameter of my function ArrayOfDirContents.

int main(int argc, char *argv[])
{
    double minimum = 10;
    double input;
    char* filename;
    wstring adr(L".");
    const wchar_t* adresar; 

    adresar = adr.c_str();

    char *p;

    int num;


    errno = 0;
    long conv;

    for (unsigned i = 1; i < argc; i++) {   
        conv = strtol(argv[i], &p, 10);

        // Check for errors: e.g., the string does not represent an integer
        // or the integer is larger than int
        if (errno != 0 || *p != '\0' || conv > INT_MAX) {
            // Put here the handling of the error, like exiting the program with
            // an error message
            cout << "";
        }
        else if(strcmp(argv[1], "/a") == 0) {
            // No error
            num = conv;
            minimum = num;
        }
        else if(strcmp(argv[3], "/d") == 0) {
            int mbtowc(wchar_t* adresar, const char* argv[4], std::size_t n);
        }
        else {
            adresar = adr.c_str();
        }

        getchar();
    }

        string nazev_souboru;
        vector<string> strpole;
        int chyby, l;
        chyby = 0;
        bool vysledek;
        vysledek = ArrayofDirContents(adresar, nazev_souboru, strpole);

        vector<string>::iterator i;

        if (vysledek == true) {
            writeFile(nazev_souboru, strpole, chyby, l, minimum);
            writeSms(strpole, chyby, l, minimum);
            for (i = strpole.begin(); i < strpole.end(); i++) {
                cout << *i;
                cout << '\n';

            }
        }
        //SaveDirectoryContents(L".", nazev_souboru);

}

Solution

  • The C:/User part tells me you're using Windows. That's important; you should have given that information.

    You cannot control Windows user names. They can be in a format that's not representable as a char** argv. The alternative is int wmain(int argc, wchar_t** argv). This converts easily to a std::vector<std::wstring>.