Strange String/characters

Good day,

I’m reading the first 12 characters from a file on a SD card on a ethernet shield to search for a match then copy that line to another file.
I’m testing just to make sure I can find a match but I’m having a problem.
The strcmp does not find a match and I noticed the strlen has 2 extra char.
You will see in my sample that the last char is a “ACK”? Integer 6 of asci table. Can’t figure out where it is coming from.

I have include the code, test file and sample output.

Anyone seen this before?

char testupc12[13] = {'0','4','9','0','0','0','0','0','0','7','3','0'}; 

boolean Find_Record(char SDupc[]){

// Format - 049000000443,Coca-Cola Classic,12 fl oz,19,Food: Beverages,813,12/25/2011

char upc12[12];                                         // 12 Digit UPC Code.
char product[35];                                       // The item name.
char description[35];                                   // Size, weight, count, color etc
char ch;
boolean match = false;
int counter = 0;


 for (int x =0; x < 36; x++) {                                    // Initialize
    product[x] = '\0';  
    description[x] = '\0'; 
  }
for (int x =0; x < 13; x++) {                                    // Initialize
    upc12[x] = '\0';
  }

strcpy_P(prog_buffer, (char*)pgm_read_word(&(string_table[4])));                 // Open UPC file.
upcFile = SD.open(prog_buffer, FILE_READ);                                       // Open the file on the SD card for read only


if (upcFile) {
  #ifdef DEBUGSD
   Serial.println(upcFile);
  #endif
}
else {
  #ifdef DEBUGSD
    Serial.println("Error opening file.");    // If the file didn't open, print an error:
  #endif
 return false;
  }    

//Serial.println(upcFile.available());
// Let's find the upc first.

while (upcFile.available()) {      //

  Serial.println();
  while (upcFile.available()) {      // Read from the file for a matching upc code
    ch = upcFile.read();
   
    if (ch != DELIM) {                             //   
      //Serial.println(upc12[counter]);
      upc12[counter] = ch;
      counter++;
      
      }  
     else {
       
       if (strcmp(SDupc, upc12) == 0){
        Serial.println('*'); 
        Serial.println(upc12);
        match = true;
        counter = 0;
       }
       else {
        match = false;  
       
       //test output

        Serial.println("**********");
        
        Serial.print(SDupc);
        Serial.print(" - ");
         Serial.println(strlen(SDupc));
          
         Serial.print(upc12);
       Serial.print(" - ");
          Serial.println(strlen(upc12));
          
          for (int x =0; x < 13; x++) {                                    // Initialize
            Serial.print(int(upc12[x]));
            Serial.print(',');
               }

        Serial.println("----------");
        counter = 0;
       }
      break;
      }
      
  }

 
 //If no match then advance to the beginning of the next line.
  if (!match){  
     while (upcFile.available()) {
       ch = upcFile.read();
       if (ch == EOL) {
        ch = upcFile.read();
        ch = upcFile.read();
        break;   
       } 
     } 
   }
  
  }  

 upcFile.close();                            // close the file:
 
 
 }  // End of Find record.

Few line of the serial output.

049000000730 - 12
049000000443 - 14
48,52,57,48,48,48,48,48,48,52,52,51,6,----------


049000000730 - 12
049000000730 - 14
48,52,57,48,48,48,48,48,48,55,51,48,6,----------


049000000730 - 12
049000000443 - 14
48,52,57,48,48,48,48,48,48,52,52,51,6,----------

Few lines of the input file.

049000000443,Coca-Cola Classic,12 fl oz,19,Food: Beverages,852,2/8/2012|
049000000443,Coca-Cola Classic,12 fl oz,19,Food: Beverages,853,2/8/2012|
049000000730,Coca-Cola Classic,12 fl oz,30,Food: Beverages,854,2/8/2012|
049000000443,Coca-Cola Classic,12 fl oz,19,Food: Beverages,855,2/8/2012|

       if (strcmp(SDupc, upc12) == 0){

The str in this function's name means that it operates on strings. upc12 is NOT a string. The snippet posted does not indicate whether SDups is, or not.

A string is an array of chars (upc12 meets that criteria) that is NULL terminated (upc12 does NOT meet that criteria).

Thanks for the reply.

Not sure what you mean.

I call the procedure in the loop. I just run it once for testing.

Example - Find_Record(testupc12);

char testupc12[13] is an array of char as you suggest. char upc12[12] is an array of char. char SDupc[] is an array of char.

I thought strings are array of char therefore strcmp should work. Do I have to create a procedure to compare the two values?

FYI I did one test where i decrease the size of the upc12[12] array. When I reach 10, it returned a match as it should but the data was messed up.

Randy

I thought strings are array of char therefore strcmp should work.

Re-read what I wrote.

A string is an array of char THAT IS NULL TERMINATED. You are not NULL terminating your char arrays. Therefore, they are NOT strings, and you can't use strxxx functions.