Answered by:
Converting filesystem::path to basic_string<TCHAR>

Question
-
Hi,
I am porting MS ConcRT Samples from VS 2013 to VS 2015 Community Update 1. Some samples are compiling fine, some do do not.
One particular problem is with the sample 'FindString', file FileFinder.h, line 42:
#define path_t std::experimental::filesystem::path
(initially was #define path_t TR2::sys::path)
bool CheckFile(path_t& file, string_t filter) { bool bRes = false; if (filter.compare(string_t(_T("*.*"))) == 0) // Any File bRes = true; else { size_t pos = filter.rfind(_TCHAR('.')); if (filter.compare(0, pos, string_t(_T("*"))) == 0) // Any File with given extension { Line 42: string_t ext = file.extension(); if (filter.compare(pos, filter.length() - pos, ext) == 0) bRes = true; } else if (filter.compare(0, filter.length(), file.filename()) == 0) // Given Name bRes = true; } return bRes;
}
There path is std::experimental::filesystem::path, extension() is function from this header that returns path, and string_t is basic_string<TCHAR>.
In VS 2015 I am getting error C2440: Cannot convert path to basic_string<TCHAR>. VS 2013 has no objections because path is defined little bit differently as TR2::sys::path.
So how to convert filesystem::path to basic_string<TCHAR>n VS 2015?
My OS is Window 7 Pro SP1.
- Edited by Geoyar Sunday, February 21, 2016 1:31 AM
Sunday, February 21, 2016 1:29 AM
Answers
-
On 2/20/2016 8:29 PM, Geoyar wrote:
There path is std::experimental::filesystem::path, extension() is function from this header that returns path, and string_t is basic_string<TCHAR>.
In VS 2015 I am getting error C2440: Cannot convert path to basic_string<TCHAR>.Are you building a Unicode build? You should. std::experimental::filesystem::path always uses Unicode (path::value_type is wchar_t, pat::string_type is std::wstring), so your code can only work when TCHAR is also wchar_t.
https://msdn.microsoft.com/en-us/library/hh874769.aspx#Anchor_52
Sunday, February 21, 2016 4:42 AM -
Thanks for reply, it was very helpful.
Indeed, the project was using multi-byte character set.
When I change it to UNICODE, I got a lot of errors and warning, because in FileFinder. h were definitions:
#ifdef UNICODE #define path_t tr2::sys::wpath #define directory_iterator_t wdirectory_iterator #else #define path_t std::experimental::filesystem::path #define directory_iterator_t directory_iterator #endif
I changed it to just that:
#define path_t std::experimental::filesystem::path #define directory_iterator_t directory_iterator
After that I had only one error related to function MyPath = filePath.file_name() which needed to be replaced with MyPath = filePath.filename().
So it is resolved.
Bit it is not my code. I suppose it is Microsoft's. Is this package, ConcRT Samples, discontinued?
Sunday, February 21, 2016 11:11 PM
All replies
-
On 2/20/2016 8:29 PM, Geoyar wrote:
There path is std::experimental::filesystem::path, extension() is function from this header that returns path, and string_t is basic_string<TCHAR>.
In VS 2015 I am getting error C2440: Cannot convert path to basic_string<TCHAR>.Are you building a Unicode build? You should. std::experimental::filesystem::path always uses Unicode (path::value_type is wchar_t, pat::string_type is std::wstring), so your code can only work when TCHAR is also wchar_t.
https://msdn.microsoft.com/en-us/library/hh874769.aspx#Anchor_52
Sunday, February 21, 2016 4:42 AM -
Thanks for reply, it was very helpful.
Indeed, the project was using multi-byte character set.
When I change it to UNICODE, I got a lot of errors and warning, because in FileFinder. h were definitions:
#ifdef UNICODE #define path_t tr2::sys::wpath #define directory_iterator_t wdirectory_iterator #else #define path_t std::experimental::filesystem::path #define directory_iterator_t directory_iterator #endif
I changed it to just that:
#define path_t std::experimental::filesystem::path #define directory_iterator_t directory_iterator
After that I had only one error related to function MyPath = filePath.file_name() which needed to be replaced with MyPath = filePath.filename().
So it is resolved.
Bit it is not my code. I suppose it is Microsoft's. Is this package, ConcRT Samples, discontinued?
Sunday, February 21, 2016 11:11 PM