Convert LPTSTR to string or char * to write to file

I want to convert LPTSTR to a string or char * so that I can write it to a file using a stream.

Any ideas?

+3
source share
3 answers

Most of the solutions presented in other threads are unnecessarily converted to obsolete encoding instead of Unicode encoding. Just use reinterpret_cast<const char*>UTF-16 to write files or convert to UTF-8 with WideCharToMultiByte.

, LPTSTR LPWSTR , Windows 9x . LPWSTR " " (, UTF-16), WCHAR wchar_t.

, ( ) UTF-16 UTF-32 ( Linux/OS X):

#include <fstream>
#include <string>

int main() {
  std::ofstream stream("test.txt");  // better use L"test.txt" on Windows if possible
  std::wstring string = L"Test\n";
  stream.write(reinterpret_cast<const char*>(string.data()), string.size() * sizeof(wchar_t));
}
+1

T2A .

+3

IIUC, LPTSTTR char wchar_t . , std::ofstream std::wofstream, .
. , TCHAR, .

0
source

Source: https://habr.com/ru/post/1755459/


All Articles