简体   繁体   English

从txt文件读取文本并将其放在静态文本字段中,MFC

[英]Reading text from txt file and putting it in static text field, mfc

I read one line of text from external .txt file, and when I try to put it in static text field of a dialog via SetWindowText with: 我从外部.txt文件中读取了一行文本,当我尝试通过SetWindowText使用以下命令将其放在对话框的静态文本字段中时:

string line;

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

staticText.SetWindowText(_T(line));

I get the following error: 我收到以下错误:

Error: identifier "Lline" is undefined. 错误:标识符“ Lline”未定义。

Is there any way to read string from .txt file and putting it to static text field? 有什么方法可以从.txt文件中读取字符串并将其放入静态文本字段?

The problem you are having is the macro _T is defined like: 您遇到的问题是宏_T的定义如下:

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

So since _UNICODE is defined 所以既然定义了_UNICODE

staticText.SetWindowText(_T(line));

Is being converted to 正在转换为

staticText.SetWindowText(Lline);

Which is giving you the undeclared identifier. 这为您提供了未声明的标识符。

You can either convert the the std::string into a TCHAR* using one of the answer on Converting string to tchar in VC++ or you could use a std::wstring to store the line and a std::wifstream to read from the file. 您可以使用VC ++中将字符串转换为tchar的答案之一将std::string转换为TCHAR* ,也可以使用std::wstring存储行,并使用std::wifstream从文件中读取。 If you do this then: 如果执行此操作,则:

staticText.SetWindowText(_T(line));

Would become 会成为

staticText.SetWindowText(line.c_str());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM