如何将const char *附加到const char * [英] How to append const char* to a const char*

查看:63
本文介绍了如何将const char *附加到const char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将三个不同的const char *变量附加到一个变量中.这是因为Windows库中的函数采用参数LPCTSTR.我有以下代码:

I am trying to append three different const char* variables into one. This is because a function from windows library takes the parameter LPCTSTR. I have the following code:

const char* path = "C:\\Users\\xxx\\Desktop\\";
const char* archivo = "vectors";
const char* extension = ".txt";

const char* fullPath =+ path;
fullPath =+ archivo;
fullPath =+ extension;

运行它时,我只会将最后一个(扩展名)添加到FullPath中.

When I run it I get only the last (extension) added to to FullPath.

推荐答案

您需要分配一些空间来容纳串联的字符串.幸运的是,C ++具有 std :: string 类可以为您执行此操作.

You need to allocate some space to hold the concatenated strings. Fortunately, C++ has the std::string class to do this for you.

std::string fullPath = path;
fullPath += archivo;
fullPath += extension;
const char *foo = fullPath.c_str();

请注意,包含连接字符串的空间由 fullPath 拥有,并且指针 foo 仅在 fullPath 为范围内,并且在调用 c_str 后未修改.

Be aware that the space containing the concatenated strings is owned by fullPath and the pointer foo will only remain valid so long as fullPath is in scope and unmodified after the call to c_str.

这篇关于如何将const char *附加到const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆