2017-07-26 51 views
0

我正在創建一個控制檯程序,它將通過控制檯從用戶處獲取目錄,並將其存儲在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::stringpxcCHAR*這個長轉換?

+2

「wchar_t」類型的變量只能包含單個字符。您可能正在尋找'std :: wstring' –

+0

請提供[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)。 – Steeve

回答

-1

包裝整個代碼或只是wcout(嘗試兩個,我不太確定哪個會工作......)在一個while循環。有點像這樣:

while (1 == 1) 
{ 
    wchar_t someRandomVariable; 
    wcout << "Input Directory: "; 
    wcin >> someRandomVariable; 
    wcout << "Directory: " << someRandomVariable; 

} 

希望它能起作用。它應該打印:

Input Directory: C:/Users/Skipher/Destop/test.txt 
Directory: C:/Users/Skipher/Destop/test.txt 
相關問題