您需要分配内存来接收文本,并且您需要确保文本缓冲区的数据类型与您用于写入文件的数据类型相匹配。这两个你都没有做。
试试这样的:
HWND hEdit = GetDlgItem(hWnd, EDIT_MAIN);
int len = GetWindowTextLengthA(hEdit);
std::vector<CHAR> text(len+1, 0);
GetWindowTextA(hEdit, &text[0], len);
ofstream file;
file.open(filePathName);
file << &text[0];
file.close();
或者:
HWND hEdit = GetDlgItem(hWnd, EDIT_MAIN);
int len = GetWindowTextLengthW(hEdit);
std::vector<WCHAR> text(len+1, 0);
GetWindowTextW(hEdit, &text[0], len);
wofstream file;
file.open(filePathName);
file << &text[0];
file.close();
或者甚至更像这样的东西:
HWND hEdit = GetDlgItem(hWnd, EDIT_MAIN);
int len = GetWindowTextLengthW(hEdit);
std::vector<WCHAR> text(len+1, 0);
GetWindowTextW(hEdit, &text[0], len);
int len2 = WideCharToMultiByte(CP_UTF8, 0, &text[0], len, NULL, 0, NULL, NULL);
std::vector<char> utf8(len2+1, 0);
WideCharToMultiByte(CP_UTF8, 0, &text[0], len, &utf8[0], len2, NULL, NULL);
ofstream file;
file.open(filePathName);
file << &utf8[0];
file.close();