I can read a file with Unicode path like this:
#include "iostream"
#include "windows.h"
using namespace std;
///--- variables ---///
//-- DWORD --//
DWORD dwBytesToRead = 0;
//-- HANDLE --//
HANDLE hFileHandle;
//-- wchar_t array --//
wchar_t a_wcContentsOfFile[999999] = {0};
//-- WINBOOL --//
WINBOOL wbResult;
int main()
{
hFileHandle = CreateFileW(L"D:\\Çağatay.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFileHandle != NULL)
{
wbResult = ReadFile(hFileHandle, a_wcContentsOfFile, 999999 - 1, &dwBytesToRead, NULL);
if(wbResult != 0)
{
if(dwBytesToRead > 0)
{
a_wcContentsOfFile[dwBytesToRead + 1] = '\0';
wprintf(L"%s\n", a_wcContentsOfFile + 1);
}
}
CloseHandle(hFileHandle);
}
getchar();
return 0;
}
I have used this website as guideline:
https://www.installsetupconfig.com/win32programming/windowsfileapis4_22.html
But I want to write contents of the file to wstring, not stdout.
How can I do this?
std::wifstream? Re: " I want to write contents of the file to wstring" - That's a one-liner usingstd::wifstream.ifstreamdoes not support non-ASCII paths. For exampleÇandğare not ASCII.//-- HANDLE --//followed byHANDLE hHandle;is just distracting. Not to mention the namehHandle, which says nothing. Windows programs tend to use prefixes likehto describe the type of the variable (hstands forHANDLE), but many folks these days things that's just noise. With or without that prefix, though, give the variable a name that says what it does.hHandleshould be something likefileHandle.std::wifstream is(std::filesystem::path(L"D:/Çağatay.txt"));and thenstd::wstring content(std::istreambuf_iterator<wchar_t>(is), std::istreambuf_iterator<wchar_t>{});- Done