sscanf storing first byte but then back to zero

I'm trying to read network parameters from a "config.txt" file on a sd card.
It reads properly and the values are being stored into variables.

The problem is that it converts the first byte of each array to zero.
The first set of values are my var netBuffer read from txt, the second set are the values stored in the variables.

mac 12:34:56:78:90:AB
ip 192.168.2.2
netmask 255.255.255.0
gateway 192.168.2.1
dns 192.168.2.1

mac 0:34:56:78:90:AB
ip 0.168.2.2
netmask 0.255.255.0
gateway 0.168.2.1
dns 192.168.2.1

The content of the txt file is:

12:34:56:78:90:AB
192.168.2.2
255.255.255.0
192.168.2.1
192.168.2.1

The code is:

#include <SPI.h>
#include <SD.h>

byte myMac[7];
byte myIP[4];
byte myNM[4];
byte myGW[4];
byte myDNS[4];

const int chipSelect = 4;

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ;
  }

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    while (1);
  }

  File dataFile = SD.open("config.txt");
  char netBuffer[32];

  int chPos = 0;
  int lineNo = 0;

  if (dataFile) {
    while (dataFile.available()) {

      char ch = dataFile.read();
      if (ch == '\r') {
        chPos = 0;

        switch (lineNo) {
          case 0:
            Serial.print("mac ");
            sscanf(netBuffer, "%2x:%2x:%2x:%2x:%2x:%2x", &myMac[0], &myMac[1], &myMac[2], &myMac[3], &myMac[4], &myMac[5]);
            break;

          case 1:
            Serial.print("ip ");
            sscanf(netBuffer, "%u.%u.%u.%u", &myIP[0], &myIP[1], &myIP[2], &myIP[3]);
            break;

          case 2:
            Serial.print("netmask ");
            sscanf(netBuffer, "%u.%u.%u.%u", &myNM[0], &myNM[1], &myNM[2], &myNM[3]);
            break;

          case 3:
            Serial.print("gateway ");
            sscanf(netBuffer, "%u.%u.%u.%u", &myGW[0], &myGW[1], &myGW[2], &myGW[3]);
            break;

          case 4:
            Serial.print("dns ");
            sscanf(netBuffer, "%u.%u.%u.%u", &myDNS[0], &myDNS[1], &myDNS[2], &myDNS[3]);
            break;
        }

        Serial.println(netBuffer);

        lineNo++;

      } else if (ch == '\n') {
        // do nothing
      } else if (chPos < 31) {
        netBuffer[chPos] = ch;
        chPos++;
        netBuffer[chPos] = 0;
      }

    }
    dataFile.close();
  }

  int x;

  Serial.println("mac ");
  for (x = 0; x < 6; x++) {
    Serial.print(myMac[x], HEX);
    if (x < 5) Serial.print(":");
  }

  Serial.println("ip ");
  for (x = 0; x < 4; x++) {
    Serial.print(myIP[x], DEC);
    if (x < 3) Serial.print(".");
  }

  Serial.println("netmask ");
  for (x = 0; x < 4; x++) {
    Serial.print(myNM[x], DEC);
    if (x < 3) Serial.print(".");
  }

  Serial.print("gateway ");
  for (x = 0; x < 4; x++) {
    Serial.print(myGW[x], DEC);
    if (x < 3) Serial.print(".");
  }

  Serial.print("dns ");
  for (x = 0; x < 4; x++) {
    Serial.print(myDNS[x], DEC);
    if (x < 3) Serial.print(".");
  }
}

void loop() {
}

Can anyone please help me figure out what am I doing wrong?

Put in some print statements to see what is going wrong.

For example, print out the contents of netBuffer in HEX format, just after this bit of code. With sscanf(), every single character has to have a match with your format string. A stray line feed or other wrong character will prevent it from working properly.

  if (ch == '\r') {
        chPos = 0;

Also, the return value of sscanf() tells you how many variables it successfully converted. It that is not equal to the number of variables supplied, obviously something is wrong with the input c-string or the format string.

jremington:
Put in some print statements to see what is going wrong.

For example, print out the contents of netBuffer in HEX format, just after this bit of code. With sscanf(), every single character has to have a match with your format string. A stray line feed or other wrong character will prevent it from working properly.

  if (ch == '\r') {

chPos = 0;




Also, the **return value** of sscanf() tells you how many variables it successfully converted. It that is not equal to the number of variables supplied, obviously something is wrong with the input c-string or the format string.

Thanks for your help

The following result is:

netbuffer in HEX
netbuffer
sscanf count
31:32:3A:33:34:3A:35:36:3A:37:38:3A:39:30:3A:41:42:0:6:49:0:0:0:6:0:0:0:FFFFFF9F:4:31:46:1
mac 12:34:56:78:90:AB
6
31:39:32:2E:31:36:38:2E:32:2E:32:0:39:30:3A:41:42:0:6:49:0:0:0:6:0:0:0:FFFFFF9F:4:31:46:1
ip 192.168.2.2
4
32:35:35:2E:32:35:35:2E:32:35:35:2E:30:0:3A:41:42:0:6:49:0:0:0:6:0:0:0:FFFFFF9F:4:31:46:1
netmask 255.255.255.0
4
31:39:32:2E:31:36:38:2E:32:2E:31:0:30:0:3A:41:42:0:6:49:0:0:0:6:0:0:0:FFFFFF9F:4:31:46:1
gateway 192.168.2.1
4
31:39:32:2E:31:36:38:2E:32:2E:31:0:30:0:3A:41:42:0:6:49:0:0:0:6:0:0:0:FFFFFF9F:4:31:46:1
dns 192.168.2.1
4

well i noticed something else:

case 1:
            Serial.print("ip ");
            sscanf(netBuffer, "%u.%u.%u.%u", &myIP[0], &myIP[1], &myIP[2], &myIP[3]);
            break;

myMac[0] before case 1 sscanf is equal to 12 and after case 1 sscanf it is equal to 0.

I can't figure what kind of sorcery is happening here.

You have defined myMac[], etc. as byte values.

Most likely, sscanf() is assuming 16bit (or larger) integers and writing to out of bounds memory locations.

I suggest to make those variables int, or use a temporary integer array for input, or modify the format string to specify single byte variables according to the "subspecifier" tables in this reference.