需要将两个字符串与NULL字符连接 [英] Need to concatenate two strings with NULL characters

查看:95
本文介绍了需要将两个字符串与NULL字符连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试开发数据库迁移实用程序.
我正在使用以下功能来创建DSN.

SQLConfigDataSource(NULL,ODBC_ADD_DSN,"SQL Server",ss)

此功能的最后一个参数是连接字符串.它的数据类型为LPCSTR.
此函数期望使用NULL字符作为分隔符.

简而言之,它期望字符串采用以下格式.
"ExpTempSQL \ 0SERVER =(本地)\ 0Trusted_Connection =是\ 0 \ 0"

如果您发现它们之间有NULL字符(\ 0).由于这个原因,我无法动态创建此字符串,因为strcat函数无法将字符串与NULL字符连接在一起,它会跳过NULL之后的字符串(我认为这是正确的行为).

在这方面的各种帮助将不胜感激.

Hi,

I am trying to develop a database migration utility.
I am using following function to create DSN.

SQLConfigDataSource(NULL, ODBC_ADD_DSN,"SQL Server",ss)

Last parameter of this fucntion is a connection string. It is of data type LPCSTR.
This function expects NULL character as a seperator.

In short it expects the string in following format.
"ExpTempSQL\0SERVER=(local)\0Trusted_Connection=yes\0\0"

If you notice there are NULL characters (\0) in between. Due to this i am not able to create this string dynamically as strcat function cannot concatenate the string with NULL characters, It skips the string beyond NULL(which is i think the correct behavior).

Help of any sort in this regard will be highly appreciated.

推荐答案

我遇到了同样的问题.您可以执行以下操作.

这是用于MFC的.

I had the same problem. You can do something like this.

This is for MFC.

CString sAttributes;
	
//Form the attributes string
sAttributes += "DSN=";
sAttributes += YOUR DSN NAME;
sAttributes += ''\0'';
sAttributes += "Server=";
sAttributes += SERVER NAME;
sAttributes += ''\0'';



您可以对其他属性执行相同的操作.



You can do the same for extra attributes.


struct testfunctor
{
void operator()(char& c) { if(c == ''|'') c = ''\0''; }
};
std::for_each( str.begin(), str.end(), testfunctor() );


这应该可行.


This should work..


是否可以使用指向字符串的指针自己复制它,然后复制其中的每个字符,并在收到两个NULL字符时停止行?
Isn''t it possible to copy it yourself using a pointer to the string and then copy every character in it and to stop when you receive two NULL characters in a row?


这篇关于需要将两个字符串与NULL字符连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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