C++ 中连接两个 char* 的函数
首先,我是一名 C# 程序员,因此我对 C++ 的工作知识相当有限。我在大学时就学过它,但已经有十年没有碰过它了,所以如果这是相对简单的东西,请原谅我。
我正在尝试制作一个可以在实现 libwpd 库的 C# 中使用的 DLL。
我已经成功创建了一个 DLL,它导出了 2 个可以通过 P/Invoke 访问的函数。第一个返回一个常量整数(由 Visual Studio 作为示例生成),第二个返回一个字符串。
如果我从函数返回一个常量字符串,它会成功传递到 C#,并且我可以在另一端读取它,因此我知道数据正在被传回。
我遇到的问题是 libwpd。我必须修改他们的 TextDocumentGenerator.cpp 文件以将信息添加到 char* 而不是使用他们使用的 printf,以便我稍后可以访问它。
我已将变量定义添加到头文件的公共部分,以便我可以从调用代码中读取它。
现在,我正在尝试编写一个函数,允许我将 libwpd 给出的 char* 添加到外部 char*。
我想出了这个:
char* addString(const char* addThis, char* toThis)
{
char* copier = (char*)malloc(strlen(toThis) + 1 + 1);
strcpy(copier, toThis);
strcpy(copier, "1");
toThis = (char*)malloc(strlen(copier) + 1);
strcpy(toThis, copier);
return copier;
}
但是当我传回信息时,我得到一个空白字符串。
我通过调用 totalFile = addString("\n",totalFile);
来调用该函数
(我意识到它应该只在技术上重复向字符串添加“1”,但它甚至没有这样做)
如果我将复印机行的 strcpy 更改为 strcat,它会锁定。
我不知道如何用 C++ 创建程序,所以我什至可以单步执行函数来查看发生了什么。
任何帮助将不胜感激。
First off, I'm a C# programmer, so my working knowledge of C++ is fairly limited. I took it back in college, but haven't touched it in 10 years, so please forgive me if this is relatively simple stuff.
I'm attempting to make a DLL that I can use in C# that implements the libwpd library.
I've managed to create a DLL that exports 2 functions that I can access via P/Invoke. The first returns a constant integer (generated by visual studio as a sample), the 2nd a string.
If I return a constant string from the function, it passes successfully to C# and I can read it on the other end, so I know the data is being passed back.
The problem I'm running into is with libwpd. I've had to modify their TextDocumentGenerator.cpp file to add the information to a char* instead of using the printf that they use so I can access it later.
I've added a variable definition to the public section of the header file so I can read it from the calling code.
Now, I'm trying to write a function that allows me to add the char* given by libwpd to the external char*.
I've come up with this:
char* addString(const char* addThis, char* toThis)
{
char* copier = (char*)malloc(strlen(toThis) + 1 + 1);
strcpy(copier, toThis);
strcpy(copier, "1");
toThis = (char*)malloc(strlen(copier) + 1);
strcpy(toThis, copier);
return copier;
}
But when I pass the information back, I get a blank string.
I call the function by calling totalFile = addString("\n", totalFile);
(I realize it should only technically add "1" to the string repeatedly, but it's not doing even that)
If i change the strcpy to strcat for the copier lines, it locks up.
I don't know how to create a program in C++ so I can even step through the functions to see what's happening.
Any assistance would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您知道 std::string 的存在吗?它是一个在C++中处理字符串的类;
char *
是 C 语言遗留下来的。std::string
提供了+
运算符,可以满足您的需求。Are you aware of existence of
std::string
? It's a class that handles strings in C++;char *
is legacy from C.std::string
provides+
operator, that does what you want.您需要为返回字符串分配足够的空间,将初始字符串复制到目标缓冲区,然后调用 strcat 附加额外信息。
例如:
不要忘记在调用此函数后的某个时刻您需要调用
free(destination)
。编辑:
当然,这只是我修复你问题中提出的功能。实际上,从函数返回指针并指望调用者释放它并不是一个好主意。由于您使用的是 C++ 而不是 C,因此使用一些 C++ 构造可能会更好,例如 std::string 或至少将 char* 包装在 shared_ptr 或类似的中。
如果您要编写 C++,我建议您自己购买 如果你不知道自己在做什么,就很容易搬起石头砸自己的脚。
You need to allocate enough space for the return string, copy the inital string into the destination buffer, and then call strcat to append the extra info.
For example:
Don't forget you'll need to call
free( destination )
at some point after calling this function.EDIT:
Of course, this is just me fixing the function proposed in your question. Really, it's not a great idea to return a pointer from a function and count on the caller to free it. Since you're using C++ rather than C, you'd likely be much better off using some C++ constructs, such as std::string or at the very least, wrapping the char* in a shared_ptr or similar.
If you're going to be writing C++, I'd recommend buying yourself a good book on the subject, it really is far too easy to shoot yourself in the foot if you don't know what you're doing.
由于您要替换
printf()
调用,因此sprintf()
可能是创建字符串的最简单方法。Since you are replacing a
printf()
call,sprintf()
would probably be the easiest way to create your string.首先,格里维斯是正确的。你没有义务做你正在做的事情并将这个问题标记为 C++。原因很简单,这不是我们在 C++ 中处理字符串的方式。
作为一名 C# 程序员,您非常了解
String
类。嗯,std::string 是类似的。它省去了处理字符串的麻烦,这样您就不必走您正在走的痛苦路线。要在 C++ 中将两个字符串连接在一起,就像这样简单:
瞧。
First off, Griwes is correct. You have no business doing what you are doing and marking this question as C++. The simple reason being, this is not how we work with strings in C++.
As you are a C# programmer you are well aware of the
String
class. Well,std::string
is similar. It takes the leg-work out of dealing with strings so that you do not have to go down the painful route that you're going down.To concatenate two strings together in C++, it's as simple as:
Voila.