簡體   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