Condition stops file from opening

Good Afternoon everyone,
i'm pretty new to Arduino, but i know a bit of C, i have a problem with my code
(btw, Arduino Ethernet, using a 32GB microSD, i do write and read without problems)
CODE:
// i open the sd correctly into the setup(), i do use SDFat.h,
void loop()
{
if (!myFile.open("stato.txt", O_WRITE | O_CREAT | O_AT_END)) sd.errorHalt("opening logging file for write failed");
if (myFile.isOpen()){

//if(k%100==0){

val=analogRead(0);
val2=analogRead(1);

myFile.print(k);
myFile.print(";");
myFile.print(val);
myFile.print(";");
myFile.print(val2);
myFile.println();

lcd.print(val);
myFile.close();

cout<<k<<";"<<val<<";"<<val2<<"\n";

// }
k++;
}
}
i wanna write when k is 100,200,300 ecc.
the problem is that if i insert the commented code, the file won't even open.
how can a the "2nd if" stop the file from opening?
if i comment the k%100 condition the sketch writes correctly

if i try to open the file I see
0;437;168
0;437;168
meaning that k isn't incremented but it actually cycles 2 times before he stops writing.

thank you for your support

Please post all your code.
Please use code tags.

Oooops, sorry about that

const uint8_t chipSelect = 4;

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

LiquidCrystal lcd(12, 11, 9, 8, 7, 6);

SdFat sd;
SdFile myFile;

int n,val,val2,k=0;
char s[4][30];

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

EthernetServer server(80);
#define error(s) sd.errorHalt_P(PSTR(s))
ArduinoOutStream cout(Serial);
void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
 
  pinMode(10, OUTPUT); 
  digitalWrite(10, HIGH);
 
  if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();
 
  SdFile rdfile("network.txt", O_READ);
  
  if (!rdfile.isOpen()) {
    cout <<("Failed opening configuration file");
  }
  

  while((n = rdfile.fgets(s[k], sizeof(s[k]))) > 0){
  //cout <<s[k];
  k++; 
  }
  
  sscanf(s[0],"%2x:%2x:%2x:%2x:%2x:%2x",&myMac[0],&myMac[1],&myMac[2],&myMac[3],&myMac[4],&myMac[5]);
  sscanf(s[1],"%u.%u.%u.%u",&myIP[0],&myIP[1],&myIP[2],&myIP[3]);
  sscanf(s[2],"%u.%u.%u.%u",&myNM[0],&myNM[1],&myNM[2],&myNM[3]);
  sscanf(s[3],"%u.%u.%u.%u",&myGW[0],&myGW[1],&myGW[2],&myGW[3]);
  Ethernet.begin(myMac,myIP,myGW,myNM);
  server.begin();
  lcd.begin(16, 1);
  Serial.println(Ethernet.localIP());
  
  k=0;
}

void loop()
{
   if (!myFile.open("stato.txt", O_WRITE | O_CREAT | O_AT_END))  sd.errorHalt("opening logging file for write failed");
   if (myFile.isOpen()){
     
     //if(k%100==0){
       
      val=analogRead(0);
      val2=analogRead(1);  
      
      myFile.print(k);
      myFile.print(";");
      myFile.print(val);
      myFile.print(";");
      myFile.print(val2);
      myFile.println();

      lcd.write(val);
      myFile.close();
     
      cout<<k<<";"<<val<<";"<<val2<<"\n";
      
   //}
   k++;
 }
  EthernetClient client = server.available();
  
}
  while((n = rdfile.fgets(s[k], sizeof(s[k]))) > 0){
  //cout <<s[k];
  k++; 
  }

Assuming that there is room in the array is stupid.

Using one letter global variables, for multiple purposes, is too.

  EthernetClient client = server.available();

What's this for?