我正在創建一個控制檯程序,它將通過控制檯從用戶處獲取目錄,並將其存儲在wchar_t變量中。如何將來自控制檯的輸入存儲到wchar_t變量?
例如,這可能是來自用戶的可能輸入:
C:/Users/Skipher/Destop/test.txt
我發現std::wcin
,似乎已經寫了一些東西到wchar_t的變量。但是,當我嘗試使用std::wcout
再次將其打印到控制檯時,它僅在整個目錄中打印出C
。
我寫錯了wchar_t變量嗎?我看到wchar_t變量的值爲L"some kind of string"
。我怎樣才能確保我的價值得到正確寫入?
std::string stringPath;
std::cin.ignore();
std::getline(std::cin, stringPath);
std::wstring wstrPath = std::wstring(stringPath.begin(), stringPath.end());
const wchar_t* wcharPath = wstrPath.c_str();
mFilePath = (pxcCHAR*)wcharPath;
在調試過程中,我看到wstrPath有我想要的值:L"C:/Users/Skipher/Destop/test.txt
然而,wcharPath有0xcccccccc <Error reading characters of string.>
最後的價值,我需要的值存儲到pxcCHAR*
變量mFilePath
這是一個在SDK中提供的wchar_t *的typedef。我怎樣才能從std::string
到pxcCHAR*
這個長轉換?
「wchar_t」類型的變量只能包含單個字符。您可能正在尋找'std :: wstring' –
請提供[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)。 – Steeve