【发布时间】:2015-01-05 13:11:10
【问题描述】:
关于如何将文本框的内容保存到文件,我尝试了各种代码变体,例如:
std::string str; // same problem with std::wstring
GetWindowTextW(hwndEdit, str.c_str(), 0); // same problem with GetWindowText
std::ofstream file;
file.open("myfile.txt");
file << str;
file.close();
但它们都存在某种字符串变量转换错误:
main.cpp(54) : error C2664: 'GetWindowTextW' : cannot convert parameter 2 from 'const char *' to 'LPWSTR'
如何使用GetWindowText 或GetWindowTextW?
注意:我不知道长度:它可以是 128 以及 1167 字符或更多。
【问题讨论】:
-
您将宽文本保存到 (a) 一个窄字符串中,该字符串 (b) 是只读的,并且 (c) 长度为零,但 所有 都是错误的。而且你可以提前知道长度,看;
GetWindowTextLengthA和GetWindowTextA用于可能满足您需求的狭窄功能。 -
感谢@WhozCraig,我尝试
std::string str (GetWindowTextLengthA(hwndEdit));创建一个可以包含正确长度的字符串,但仍然存在一些错误。你想知道答案吗?我正在浏览许多/字符串教程文档,但无法完成这项工作。