C++ visualc&x2B+;将Char转换为std::string

C++ visualc&x2B+;将Char转换为std::string,c++,char,stdstring,C++,Char,Stdstring,我正在尝试获取环境用户名并将文件下载到C:\username.txt 这是我的密码: #include <tchar.h> #include <urlmon.h> #include <windows.h> #include <Lmcons.h> #include <string> #include <sstream> #pragma comment(lib, "urlmon.lib") int main(int argc,

我正在尝试获取环境用户名并将文件下载到C:\username.txt 这是我的密码:

#include <tchar.h>
#include <urlmon.h>
#include <windows.h>
#include <Lmcons.h>
#include <string>
#include <sstream>
#pragma comment(lib, "urlmon.lib")

int main(int argc, char* argv[])
{
        TCHAR username[UNLEN + 1];
        DWORD size = UNLEN + 1;
        GetUserName((TCHAR*)username, &size);
        std::string pref;

        path = "C:\"+username+".txt";

        HRESULT hr1 = URLDownloadToFile ( NULL, _T("http:// example.com/test.txt"), _T(path), 0, NULL );
        return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“urlmon.lib”)
int main(int argc,char*argv[])
{
TCHAR用户名[UNLEN+1];
DWORD大小=UNLEN+1;
GetUserName((TCHAR*)用户名和大小);
std::字符串pref;
path=“C:\”+用户名+”.txt”;
HRESULT hr1=URLDownloadToFile(NULL,_T(“http://example.com/test.txt”),_T(path),0,NULL);
返回0;
}

但我不知道如何将用户名转换成字符串。当我在路径中使用字符串名称时,也会出现错误。但是,当我使用“”并手动写入路径时,没有问题。谢谢

路径的变量声明在哪里?它也应该是
“C:\\”
,因为\在string literals中有特殊的含义。您似乎误解了
\u T
。对于Windows API调用,只需使用宽字符串即可。使用ANSI版本进行winapi调用是毫无意义的。变量
path
在哪里?