Reading from SD card overwrite values

Hi Guys,
i want to store ip, netmask and gateway from cfg/network.cfg on SD card. I'm able to read these values, but from some reason value mask[0] is overwriten with 0.
I don't see where could be problem. Here is the code:

#include <SdFat.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[4] ={0};
byte gw_ip[4] = {0};
byte mask[4] = {0};

char cfg_file[]="cfg/network.cfg";

EthernetServer server(8080);

#define REQ_BUF_SZ   100
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP req
SdFat sd;

#define error(s) sd.errorHalt_P(PSTR(s))

void setup() {
  Serial.begin(9600);

  pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
  digitalWrite(10, HIGH); // but turn off the W5100 chip!

  if (!sd.begin(4, SPI_HALF_SPEED)) sd.initErrorHalt();
  

  read_cfg();

  Ethernet.begin(mac,ip,gw_ip,gw_ip,mask);
  Serial.println(Ethernet.localIP());
  Serial.println(Ethernet.subnetMask());
  Serial.println(Ethernet.gatewayIP());
  Serial.println(Ethernet.dnsServerIP());
  server.begin();
  Serial.println("server begin");
  delay(200);
}

void loop()
{

}

void read_cfg(){
  
 char junk[10];
 ifstream sdin(cfg_file);
 if (!sdin.is_open()) error("open");
 
  while (sdin.getline(HTTP_req, REQ_BUF_SZ, '\n'))
  {
     Serial.println(HTTP_req);
     if(strncmp(HTTP_req, "ip",2) == 0)
     {
       sscanf(HTTP_req, "%2s=%u.%u.%u.%u", &junk, &ip[0], &ip[1], &ip[2], &ip[3]);
     }
     else if(strncmp(HTTP_req, "netmask", 7) == 0)
     {
        sscanf(HTTP_req, "%7s=%u.%u.%u.%u", &junk, &mask[0], &mask[1], &mask[2], &mask[3]);
     }
     else if(strncmp(HTTP_req, "gateway", 7) == 0)
     {
        sscanf(HTTP_req, "%7s=%u.%u.%u.%u", &junk, &gw_ip[0], &gw_ip[1], &gw_ip[2], &gw_ip[3]);
     }

     // More record checking/parsing
  }
}

cfg/network.cfg file I have:
ip=10.0.55.80
netmask=255.255.255.0
gateway=10.0.55.1

And when I run this program output is as following:

ip=10.0.55.80
netmask=255.255.255.0
gateway=10.0.55.1
10.0.55.80
0.255.255.0
10.0.55.1
10.0.55.1
server begin

As you can see, first part of netmask is 0.
Could you provide me some help? Thank you.

Without digging into this too much, i'd like to just let you know, that using arrays in Arduino is slightly different thing than in other languages. When you assign the array size, let's say to be array[4], you will get 4 possible places in the array to write into/read from; 1, 2, 3 and 4. You won't be able to write into the array[0].

EDIT: i wanted to add a note, that this is my own experience and probably not real information / true

change:

sscanf(HTTP_req, "%7s=%u.%u.%u.%u", &junk, &mask[0], &mask[1], &mask[2], &mask[3]);

==>

int x = sscanf(HTTP_req, "%7s=%u.%u.%u.%u", &junk, &mask[0], &mask[1], &mask[2], &mask[3]);
Serial.print(">>>>");
Serial.println(x);
Serial.print(">>>>");
Serial.println(mask[0]);

sscanf returns the number of converted fields,
and you need to check the mask[0] immediately after the scanf.

If it is right there it is overwritten in an later statement.

When you assign the array size, let's say to be array[4], you will get 4 possible places in the array to write into/read from; 1, 2, 3 and 4. You won't be able to write into the array[0].

definitely not true. Otherwise the other fields would fail too!

I made that changes as you suggested and directly after scanf that value was ok. I made next change>

       int x=sscanf(HTTP_req, "%7s=%u.%u.%u.%u", &junk, &mask[0], &mask[1], &mask[2], &mask[3]);
       Serial.print("con_field:");
       Serial.println(x);
       Serial.print("mask[0]:");
       Serial.println(mask[0]);
     }
     else if(strncmp(HTTP_req, "gateway", 7) == 0)
     {
        // buffer contains ipvalues
       Serial.print("mask[0]:");
       Serial.println(mask[0]);
       int x = sscanf(HTTP_req, "%7s=%u.%u.%u.%u", &junk, &gw_ip[0], &gw_ip[1], &gw_ip[2], &gw_ip[3]);
       Serial.print("con_field:");
       Serial.println(x);
       Serial.print("mask[0]:");
       Serial.println(mask[0]);
     }

and result is this:

ip=10.0.55.80
netmask=255.255.255.0
con_field:5
mask[0]:255
gateway=10.0.55.1
mask[0]:255
con_field:5
mask[0]:0
My IP address:10.0.55.80
0.255.255.0
10.0.55.1
10.0.55.1

after sscanf of gateway, that value was changed to 0.

I store my network settings on a SD card now and then. I use a file called network.txt to store the settings in this order and format. There must be a newline character at the end of each line, including the last line (dns).
mac address
IP
netmask
gateway
dns

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

I use this code.

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

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

void setup() {
  Serial.begin(9600);  

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  if(!SD.begin(4)) Serial.println("SD fail");
  else Serial.println("SD ok");

  File fh = SD.open("network.txt",FILE_READ);
  char netBuffer[32];
  
  if(!fh)
  {
    Serial.println("SD open fail");
    return;    
  }

  int chPos = 0;
  int lineNo = 0;
  
  while(fh.available())
  {
    char ch = fh.read();
    if(ch == '\n') {
      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 == '\r') {
      // do nothing
    }
    else if(chPos < 31) {
      netBuffer[chPos] = ch;
       chPos++;
      netBuffer[chPos] = 0;
    }
  }
  
  fh.close();

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

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

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

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

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

  Serial.println("\r\nStarting ethernet");
  Ethernet.begin(myMac,myIP,myDNS,myGW,myNM);
  
  Serial.println(Ethernet.localIP());
}

void loop() {
}

edit: Note I do not use the SDFat library. I use the SD library.