函数连接Char数组,包括其间的附加标志

时间:2016-01-13 15:04:24

标签: c++ arrays arduino

我试图解决它半天......没有成功......

我有一个结构:

typedef struct s_iomodus {
const char* SENSOR; 
const char* POSITION_1;
const char* SHOW_MI;
const char* POSITION_2;
const char* TYPE_1;
const char* TYPE_2;
const char* DESCRIPTION;  // LOC Description of the
const int NRVALUES;
} iomodus_t;

iomodus_t iomodus[] = {
{ "Relay","WW_Tank","WW_Pumpe_An_Aus","NO_P2","NO_T1","NO_T2" ,"MAGNETIC", 1}, //D25 :
{ "Relay","Puffer_Tank","NO_SHOW","NO_P2","NO_T1","NO_T2" ,"SSR", 1}, //D26 :
{ "Relay","WW_Tank","Valve","Auslauf_unten","Zu","Auf" ,"MAGNETIC", 2}, //D27 :

其中我存储所有PINS的所有STATUS QUO设置和位置,我将其作为主题发送到MQTT服务器..

现在我必须从这个数组构建一个字符串,将其作为

发送到VOID SETUP中
void setup()
{
  Serial.begin(115200);

  MQTTclient.setServer(server, 1883);
  MQTTclient.setCallback(callback);
  Ethernet.begin(mac, ip);
  // Allow the hardware to sort itself out
  delay(1500);

 const char *c_topic = concat_strings(iomodus[i].POSITION_1,iomodus[i].SENSOR, iomodus[i].POSITION_2);

      Serial.println(c_topic);
      MQTTclient.publish(c_topic, iomodus[i].VERSION);

    }

我的问题是...该函数没有生成正确的字符串 它没有添加" /"介于两者之间! 它应该是这样的字符串: " MQTTTOPIC_PREFIX / TEXT_str1 / TEXT_str2 / TEXT_str3" 要么 " MQTTTOPIC_PREFIX / TEXT_str1 / TEXT_str2 / TEXT_str3 / TEXT_str4" 如果str4存在/不为空

const char *concat_strings(const char *str1, const char *str2, const char *str3,const char *str4)
{
  // define a buffer
  static char result[MAX_CONCAT_LEN] = {0};

  // counter part
  int i = 0;
  const char *slash = {"/"};
  const char *PREF= {MQTTTOPIC_PREFIX};
  size_t len = strlen(PREF)+strlen(str1)+strlen(str2)+strlen(str3);

  // loop until end of ID has reached or destination buffer is full
  while(*PREF && i < MAX_CONCAT_LEN)
    {result[i++] = *PREF++;}
  // loop until end of string 1 has reached or destination buffer is full
  while(*str1 && i < MAX_CONCAT_LEN)
    {result[i++] = *str1++;}
  // loop until end of string 2 has reached or destination buffer is full
  while(*str2 && i < MAX_CONCAT_LEN)
    {result[i++] = *str2++;}

if (str3==TRUE){
// loop until end of SLASH has reached or destination buffer is full
  while(*slash && i < MAX_CONCAT_LEN)
    {result[i++] = *slash++;} 
  while(*str3 && i < MAX_CONCAT_LEN)
   { result[i++] = *str3++;}
   }    

 if (str4==TRUE){
// loop until end of SLASH has reached or destination buffer is full
  while(*slash && i < MAX_CONCAT_LEN)
    {result[i++] = *slash++;} 
  while(*str4 && i < MAX_CONCAT_LEN)
   { result[i++] = *str4++;}
   }      

  result[len+1] = 0;

  return result;
}

所以即使我这样做......它也不会添加&#34; /&#34;并且如果str3或str4为FALSE则不想识别...

请加入数组并添加&#34; /&#34;最简单的方法是什么?如果str3或str4可用(str1和str2总是在那里)

提前谢谢!

2 个答案:

答案 0 :(得分:1)

代码中有几个问题:

  • 首先,最好在函数外声明result

    static char result[MAX_CONCAT_LEN] = {0};
    
  • 使用str4的默认值声明函数:

    const char *concat_strings(const char *str1, const char *str2, const char *str3,const char *str4 = NULL);
    
  • 请勿将char*TRUE进行比较,而是使用NULL

    if (str4 != NULL){
    
  • PREFstr1str2之间,没有添加分隔符的代码:

    while(*PREF && i < MAX_CONCAT_LEN)
      {result[i++] = *PREF++;}
    // loop until end of string 1 has reached or destination buffer is full
    while(*str1 && i < MAX_CONCAT_LEN)
      {result[i++] = *str1++;}
    // loop until end of string 2 has reached or destination buffer is full
    while(*str2 && i < MAX_CONCAT_LEN)
      {result[i++] = *str2++;}
    
  • str4中,slash指向字符串的结尾,原因是:

      while(*slash && i < MAX_CONCAT_LEN)
        {result[i++] = *slash++;}
    

    在上一个循环中。 (你通常不需要在这里使用循环,除非分隔符将来可能不止一个字符)

  • len存在时,
  • str4未调整:

    size_t len = strlen(PREF)+strlen(str1)+strlen(str2)+strlen(str3);
    
    result[len+1] = 0;
    

    无论如何都会切断str4

如果可能,最好使用String classstrcat()

有关使用Arduino String类进行字符串连接的一些示例,请参阅hereherehere

 // adding a constant integer to a string:
  stringThree = stringOne + 123;

  // adding a constant long interger to a string:
  stringThree = stringOne + 123456789;

  // adding a constant character to a string:
  stringThree = stringOne + 'A';

  // adding a constant string to a string:
  stringThree = stringOne +  "abc";

  // adding two Strings together:
  stringThree = stringOne + stringTwo;

答案 1 :(得分:1)

我认为你坚持使用C功能和const char*字符串。还有一些更有用的C字符串函数,如strlenstrcopy。正如您将从C API中注意到的那样,在处理原始char*字符串时,标准做法是发出一个缓冲区,函数应该将其结果写入其中,因此很清楚它的责任是清理字符串(在这种情况下:来电者的!)。因此,您可以执行以下操作(已测试here):

// Example program
#include <iostream>
#include <string>
#include <cstring>

bool strconcat(char* result, unsigned int bufsize, const char* s1, const char* s2, const char* s3, const char* s4) {
    const char* strings[4] = {s1, s2, s3, s4};
    for(unsigned char i = 0; i < 4; ++i) {
        const char* s = strings[i];
        if(!s)
            return true;
        unsigned int len = strlen(s);
        if(len > bufsize)
            return false;
        strcpy(result, s);
        bufsize -= len * sizeof(char);
        result += len * sizeof(char);
        if(i < 3 && strings[i+1]) {
            if(bufsize > 0) {
                result[0] = '/';
                --bufsize;
                ++result;
            }
            else
                return false;
        }
    }
    return true;
}

int main()
{
  char result[80] = {0};
  const char* a = "ab";
  const char* b = "xyziuehfih";
  const char* c = "ihihfeih";
  strconcat(result, 80, a, a, b, 0);
  std::cout << result << "\n";
}