我试图将三个不同的const char*变量附加到一个变量中.这是因为来自windows库的函数采用参数LPCTSTR.我有以下代码:
const char* path = "C:\\Users\\xxx\\Desktop\\";
const char* archivo = "vectors";
const char* extension = ".txt";
const char* fullPath =+ path;
fullPath =+ archivo;
fullPath =+ extension;
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我只获得添加到FullPath的最后一个(扩展名).
您需要分配一些空间来保存连接的字符串.幸运的是,C++有一个std::string类为你做这个.
std::string fullPath = path;
fullPath += archivo;
fullPath += extension;
const char *foo = fullPath.c_str();
Run Code Online (Sandbox Code Playgroud)
请注意,包含连接字符串的空间属于,fullPath并且foo只有fullPath在范围内并且在调用后未修改时,指针才会保持有效c_str.