gpt4 book ai didi

c++ - 从txt文件中读取文本并将其放入静态文本字段,mfc

转载 作者:行者123 更新时间:2023-11-28 02:20:49 26 4
gpt4 key购买 nike

我从外部 .txt 文件中读取一行文本,当我尝试通过 SetWindowText 将其放入对话框的静态文本字段时:

string line;

ifstream highscore ("highscore.txt");
if (highscore.is_open())
{
getline(highscore, line);
}

staticText.SetWindowText(_T(line));

我收到以下错误:

Error: identifier "Lline" is undefined.

有什么方法可以从 .txt 文件中读取字符串并将其放入静态文本字段?

最佳答案

您遇到的问题是宏 _T 的定义如下:

#if defined(_UNICODE)
#define _T(x) L ##x
#else
#define _T(x) x
#endif

所以因为定义了_UNICODE

staticText.SetWindowText(_T(line));

正在转换为

staticText.SetWindowText(Lline);

这给了你未声明的标识符。

您可以使用 Converting string to tchar in VC++ 上的答案之一将 std::string 转换为 TCHAR*或者您可以使用 std::wstring 来存储行并使用 std::wifstream 从文件中读取。如果你这样做,那么:

staticText.SetWindowText(_T(line));

会变成

staticText.SetWindowText(line.c_str());

关于c++ - 从txt文件中读取文本并将其放入静态文本字段,mfc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32585719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com