2017-08-07 105 views
0

如何將char* argv[]轉換爲wstring?我使用"/d"argv獲取字符串路徑,並將其作爲我的函數ArrayOfDirContents的參數插入。將char * argv []轉換爲wstring

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); 

} 
+0

你到目前爲止嘗試過什麼?實際上你想要解決的問題是什麼? (查找['std :: mbtowc'](http://en.cppreference.com/w/cpp/string/multibyte/mbtowc)。) –

+0

我有一個文件夾路徑作爲參數例如「C:/ User 「並且我需要將此字符串從argv轉換爲wstring或const wchar t –

+0

Martin Bonner爲什麼是mbtowc int?我需要將值保存爲wstring或wchart * –

回答

3

C:/User部分告訴我您正在使用Windows。這很重要;你應該已經提供了這些信息。

您無法控制Windows用戶名。它們的格式不能表示爲char** argv。替代方案是int wmain(int argc, wchar_t** argv)。這很容易轉換爲std::vector<std::wstring>