简体   繁体   English

将一个 Char 数组添加到 C++ 中的 Char * 变量

[英]Add a Char array to a Char * variable in c++

I am makinga code to send a query for my SQLlite database, but I just can't find a way to add a variable to the Char * variable that is being used as the query.我正在编写代码来发送对我的 SQLlite 数据库的查询,但我找不到将变量添加到用作查询的 Char * 变量的方法。 the query and the variable that I want to add are declared like this:我要添加的查询和变量声明如下:

String p = "10004F1F7";
 char *sql = "SELECT * from TABELTAGGEGEVENS WHERE ID =" + p;

the error i get is this: error: invalid user-defined conversion from 'Arp::BasicString' to 'char*' [-fpermissive]我得到的错误是这样的:错误:从“Arp::BasicString”到“char*”的无效用户定义转换[-fpermissive]

many thanks.非常感谢。

This doesn't work because in C++ the '+' operator on char pointers doesn't concatenate the strings.这不起作用,因为在 C++ 中,字符指针上的“+”运算符不会连接字符串。 One solution would be to make the literal value a String as well:一种解决方案是将文字值也设为字符串:

String p = "10004F1F7";
String query = "SELECT * from TABELTAGGEGEVENS WHERE ID ="; 

You can then concatenate like this: + operator: p + operator然后您可以像这样连接: +运算符: p + operator

I don't know the particular library you appear to be working with ( Arp::BasicString ), so I don't know how you'd convert that into a char *.我不知道您似乎正在使用的特定库( Arp::BasicString ),所以我不知道您如何将其转换为 char *。

With std::string you can simply call c_str on the result.使用 std::string 您可以简单地对结果调用c_str

Another and probably better solution is to use formatters.另一个可能更好的解决方案是使用格式化程序。

For reference see:参考:

String p = "10004F1F7";
char *sql = "SELECT * from TABELTAGGEGEVENS WHERE ID =" + p;

You can handle the Arp::BasicString almost like an std::string.您可以像处理std::string.一样处理Arp::BasicString std::string.

Looking through the headers you will find a Method named "CStr()" .查看标题,您会发现一个名为"CStr()"

Like that you should be able to do something like this:像这样你应该能够做这样的事情:

String P = "10004F1F7";
String Query = "SELECT * from TABELTAGGEGEVENS WHERE ID =" + p;
char *sql = Query.CStr() 

The only hit on Google for Arp::BasicString was used in the SDK for a software company, PLCnext. Arp::BasicString在 Google 上的唯一成功用于软件公司 PLCnext 的 SDK。 A little bit of riffling through I found a header file BasicString.hxx and inside a prototype for BasicString class template.稍微翻了一下,我发现了一个头文件BasicString.hxx和一个BasicString类模板的原型。 There the baseString data structure is private. baseString数据结构是私有的。

I had to come up with this (rather low-level) workaround, compiling with PLCnext software succeeded and passed tests when adjusted for std::string ):我不得不想出这个(相当低级)的解决方法,当针对std::string进行调整时,使用 PLCnext 软件编译成功并通过了测试):

String p = "10004F1F7";
const char* CMD_SEQUENCE = "SELECT * from TABELTAGGEGEVENS WHERE ID =";
const int CMD_LENGTH = 41;

// allocate and assign memory for the static characters in the command
char *sql = (char *)malloc(CMD_LENGTH * sizeof(char));
memcpy(sql, CMD_SEQUENCE, CMD_LENGTH);

// iterate through all chars in String p
// resizing the memory buffer as needed and adding ith char to the end
for (int i=0; i<p.Size();i++){

    sql = (char*)realloc(sql, (CMD_LENGTH + i) * sizeof(char));

    // destination is the ith memory cell past the cmd sequence
    int destIdx = CMD_LENGTH + i;

    // copy 1 char at a time; ith char in p
    memcpy( &sql[destIdx], &p.At(i), sizeof(char) );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM