繁体   English   中英

无法使用strcpy复制字符数组

[英]Character Array not copying using strcpy

我正在检查文本文件(使用HTTP请求)中的数字。 我想每次更改时都跟踪此数字,因此我将HTTP响应收集在字符数组中,并将其与以前存储的计数(尝试过int,现在尝试使用字符数组)进行比较,但它没有似乎正在存储结果。 lastCount是一个全局变量,此函数用于检查和存储新计数:

boolean checkCount()
{
  char currentLine[15];
  int index=0;
  if (client.connected())
  {
    Serial.println("Connected. Checking for availabilty of client...");
    if (client.available())
    {
      Serial.println("Client is available! Trying to read from client...");
      // read incoming bytes:

      if(client.find("\r\n\r\n"))
      {

      char inChar;
      while(inChar=client.read())
      {
        if(inChar==-1) break;
          currentLine[index] = inChar; //read in all characters of count
          currentLine[index+1] = '\0';
          index++;
        Serial.print("\n\nCurrent count count: ");
        Serial.print(currentLine);
        Serial.print("\n\n");
      }
      if(strcmp(currentLine,lastCount)!=0) //not equal
      {
        Serial.println("NOT EQUAL TO LAST COUNT");
        Serial.print("last count: "); //test output
        Serial.print(lastCount);
        Serial.print("current count: ");
        Serial.println(currentLine);

        strcpy(lastCount, currentLine);//, sizeof(lastCount));

        Serial.println("BLINKING LED");
        blink(3);
      }
      else
      {
        Serial.println("EQUAL TO LAST COUNT");
        Serial.println("NOT BLINKING.");
      }

      delay(5000); //delay 5 seconds (don't kill server)
      return true;
     }
     else
     {
       Serial.println("newlines not found - error with request");
       return false;
     }
    }
    else
    {
      Serial.println("count not available");
      return false;
    }
  }
  //...
}

请参见以下代码:

您应该将此'\\ 0'放在数组中字符串的末尾。

currentLine [index + 1] ='\\ 0'; 此行应从“ while循环”中调用。 否则,strcmp始终只能从“ currentLine”中获得一个字符。

并且在存储输入时请注意字符串缓冲区的数组长度

while(inChar=client.read())
{         
    currentLine[index++] = inChar; //read in all characters of count
}
currentLine[index] = '\0';

if(strcmp(currentLine,lastCount)!=0) //no

暂无
暂无

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

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