2013-04-24 225 views
3

我正在取得巨大進展,但我有兩個問題讓我放慢了幾天。最大的是我想節省FindFileData.cFileName作爲字符串,但我不能!任何幫助?無法將FindFileData.cFileName轉換爲字符串

+0

發表一些代碼!你嘗試了什麼? – 2013-04-24 15:00:39

+0

我通常會這樣做,但這次我在網上搜索可能的解決方案,我寫了一些嘗試,但他們看起來像垃圾,所以我刪除了一切,並在這裏問了問題。 – alwaystudent 2013-04-24 15:43:05

回答

1

WIN32_FIND_DATA參考頁cFileNameTCHAR[]類型。如果啓用UNICODE(TCHARwchar_t)使用std::wstring

#include <string> 
std::wstring ws(FindFileData.cFileName); 

以其它方式使用std::string(如TCHARchar):

std::string ws(FindFileData.cFileName); 

或者,要同時迎合:

std::basic_string<TCHAR> s(FindFileData.cFileName); 
// std::string is a typedef for std::basic_string<char> 
// std::wstring is a typedef for std::basic_string<wchar_t> 
+0

我到了這一點,我知道如何做到這一點.....但是如何將wstring轉換爲字符串? – alwaystudent 2013-04-24 15:40:58

+0

你爲什麼想這樣做。使用'wstring'。 – 2013-04-24 16:34:57

+2

,因爲我總是想學到比我目前所知道的更多的東西! :-) – alwaystudent 2013-04-25 09:54:22

2

我從這裏複製它:How to convert wstring into string? 它將ws直接轉換爲字符串(包括FindFileData.cFileName)。任何更好的建議或任何有用的評論?

#include <clocale> 
#include <locale> 
#include <string> 
#include <vector> 

inline std::string narrow(std::wstring const& text) 
{ 
    std::locale const loc(""); 
    wchar_t const* from = text.c_str(); 
    std::size_t const len = text.size(); 
    std::vector<char> buffer(len + 1); 
    std::use_facet<std::ctype<wchar_t> >(loc).narrow(from, from + len, '_', &buffer[0]); 
    return std::string(&buffer[0], &buffer[len]); 
}