简体   繁体   中英

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:

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.

Is there any way to read string from .txt file and putting it to static text field?

The problem you are having is the macro _T is defined like:

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

So since _UNICODE is defined

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. If you do this then:

staticText.SetWindowText(_T(line));

Would become

staticText.SetWindowText(line.c_str());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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