Alex Rivera | Logout

How do I convert from LPCTSTR to std::string?

Asked 2011-04-01T12:59:52.583
15

I have an LPCTSTR and want to call a function that takes a std::string parameter.

What conversion do I need to do?

Edit
Report

2 Answers

17

in one line:

std::string s = CT2A( lpctstr );
answered 2011-04-01T17:50:16.197
2

From your comment: "The function I am calling will take the std::string and use it as the filename when calling std::fstream::open()"

Well, that's wrong. The function really should take a tstring (defined as typedef std::basic_string<TCHAR> tstring). There are many Windows files whose name cannot be represented without Unicode. For instance, all files under \User\<myusername\My Documents\ if <myUserName> contains a non-ANSI character (and you really don't want to tell your user that he has a bad name!)

But once you've changed to tstring, it just works. You still get the same std::fstream object.

answered 2011-04-01T14:48:40.040

Your Answer