我正在取得巨大進展,但我有兩個問題讓我放慢了幾天。最大的是我想節省FindFileData.cFileName
作爲字符串,但我不能!任何幫助?無法將FindFileData.cFileName轉換爲字符串
回答
從WIN32_FIND_DATA
參考頁cFileName
是TCHAR[]
類型。如果啓用UNICODE(TCHAR
是wchar_t
)使用std::wstring
:
#include <string>
std::wstring ws(FindFileData.cFileName);
以其它方式使用std::string
(如TCHAR
是char
):
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>
我到了這一點,我知道如何做到這一點.....但是如何將wstring轉換爲字符串? – alwaystudent 2013-04-24 15:40:58
你爲什麼想這樣做。使用'wstring'。 – 2013-04-24 16:34:57
,因爲我總是想學到比我目前所知道的更多的東西! :-) – alwaystudent 2013-04-25 09:54:22
我從這裏複製它: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]);
}
- 1. 無法將零轉換爲字符串
- 2. 無法將字符串轉換爲Bool
- 3. 無法將字符串轉換爲真
- 4. 無法將event.target轉換爲字符串
- 5. 無法將HashWithIndifferentAccess轉換爲字符串?
- 6. 無法將字符串轉換爲DateTime?
- 7. 無法將字符串轉換爲Guid
- 8. 無法將anyObject轉換爲字符串
- 9. 無法將字符串轉換爲SimpleDateFormat
- 10. 將字符串轉換爲字符串後無法反轉字符串Array
- 11. 無法將字符串轉換爲字符串[]
- 12. 無法將類型'字符串'隱式轉換爲'字符串'
- 13. 無法將字符串轉換爲常量字符/字符串*爲int *
- 14. 無法將字符串轉換爲常量字符
- 15. 無法弄清楚如何將字符串轉換爲字符[]
- 16. 無法將字符串轉換爲字符
- 17. 將字符串轉換爲字符串
- 18. AnyObject是無法轉換爲字符串
- 19. 對象無法轉換爲字符串
- 20. 對象無法轉換爲字符串
- 21. 無法轉換爲字符串
- 22. 無法從'字符串'轉換爲'System.Net.Http.HttpRequestMessage'
- 23. 無法從字符串轉換爲Int
- 24. 無法轉換爲Json字符串
- 25. 對象無法轉換爲字符串
- 26. 對象無法轉換爲字符串
- 27. 對象[]無法轉換爲字符串[]
- 28. 無法轉換爲字符串
- 29. 對象無法轉換爲字符串
- 30. 對象無法轉換爲字符串
發表一些代碼!你嘗試了什麼? – 2013-04-24 15:00:39
我通常會這樣做,但這次我在網上搜索可能的解決方案,我寫了一些嘗試,但他們看起來像垃圾,所以我刪除了一切,並在這裏問了問題。 – alwaystudent 2013-04-24 15:43:05