Saving elements of an integer array to sd card

Hello everyone

I am working on a project and i need to save the elements of an integer array to sd card. I have written the code for that task and it actually works but the thing is it doesnt take the first two element correctly. Instead it saves randomly each time and also it updates the first element of that array randomly which causes problems because i use that array in different parts of my program. I really don't undestand why is that happening. (By the way it usually saves the first element as 29816 and second 256 but that is not the certain case)
Here is my code:

file=SD.open(filename,FILE_WRITE);                           //filename is a string 
          if(file){
            for(int i=0;i<arraylength;i++){ 
                file.print(intarray[i]);   
                file.print(" ");
            }
          }

Thanks for your responses...

Post the entire code, I would guess the first two elements are being overwritten by something else in the code.

david_2018:
Post the entire code

The entire code is very long like 500 lines but let me just add some comment lines and post the code that matters for this part of the program.

david_2018:
Post the entire code, I would guess the first two elements are being overwritten by something else in the code.

Hello david, i found my problem while commenting it. I was storing string filename in my code block in a char array and than i use strcat() to add something to that array but it turns out the size of the array is too small for that operation. Thanks for helping me i was about to go crazy...