【发布时间】:2014-07-31 08:29:15
【问题描述】:
我很难将字符串转换为LPWSTR,因此我可以使用PathStripToRoot() 函数。
首先,MSDN 文档说我需要 LPTSTR 变量 (http://msdn.microsoft.com/en-us/library/windows/desktop/bb773757(v=vs.85).aspx),但 Visual Studio 2013 说我需要 LPWSTR。
这是我的函数的代码sn-p:
fileStat fileCreate(const string& targetFile)
{
fileStat filez;
fstream file(targetFile.c_str());
if (!file)
{
cout << "File does not exist" << endl;
}
std::ifstream in(targetFile, ios::binary | ios::ate);
int a = in.tellg();
cout << "File size(bytes): " << in.tellg() << endl << endl;
file.close();
wstring stemp = strChange(targetFile);
LPCWSTR result = stemp.c_str();
/* Tried the below code but that did not work
LPWSTR ws = new wchar_t[targetFile.size() + 1];
copy(targetFile.begin(), targetFile.end(), ws);
ws[targetFile.size()] = 0;
*/
cout<<"\n\n"<<PathStripToRoot(ws)<<"\n\n";
...
filez.fileSize = a;
return filez;
}
很多人说要使用MultiByteToWideChar() 函数,但我查看了 MSDN 文档,不知道它是如何工作的。有没有比使用MultiByteToWideChar() 更简单的方法?
【问题讨论】:
-
文档说
LPTSTR,注意额外的T。这意味着它是否宽取决于构建配置。 -
在旁注中,我有一个从“std::streamoff 转换为 int,可能丢失数据”的警告,这是由我要存储的 int a = in.tellg() 函数引起的文件大小(以字节为单位)。
-
@dspaces1: 我刚刚删除了 [visual-studio-2013] 标签,因为虽然您肯定使用 VS2013 来开发您的代码,但可能这不是问题的关键(例如,这不是关于VS2013 IDE的一个问题......)。为了完整起见,我在您的问题文本中添加了“2013”VS 版本号。当然,如果您认为您最初的选择更好,请随时撤消我的更改。
-
@AdrianMcCarthy 我看了那篇文章,这只有助于转换为不适用于 PathStripToRoot() 的 LPCWSTR