您需要分配內存來接收文本,並且您需要確保文本緩衝區的數據類型與用於寫入文件的數據類型相匹配。兩者都不是你正在做的。
嘗試更多的東西是這樣的:
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();
你'LPTSTR'被編譯成'LPWSTR' 。歡迎使用Unicode。 –
所以....有什麼建議嗎? – Forrest4096