Bizarre SD card read/write behavior

I am using an Arduino Mega with a microSD card on SPI bus.

I want to record the last encoder position for two DC motors M1 and M2 so that upon startup, I can move the motors to the zero position.

I am trying out a script that will read one Int32_t from each file M1last and M2 last.
In trying out the code, I am first deleting the files and hard coding a value 3456789 for M1 and 9876543 for M2. The variables are separate for the M1 and M2 routines and I reset them to null/0 before the card is read.

However, when I run the script, it is assigning the value of 9876543 to M1 and 3456789 to M2 (exactly the opposite of each other.)

I can't figure out where I am going wrong !! Maybe a few sets of eyes can tell.

This is the output on the serial monitor:

Initializing SD card...initialization done.

M1 String from SD: 9876543
M1last: 9876543
M2 String from SD: 3456789
M2last: 3456789

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

String readstrM1 = "";
String readstrM2 = "";
int32_t M1last = 0;
int32_t M2last = 0;


File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  if (SD.exists("M1.txt")) {

    myFile = SD.open("M1.txt", FILE_READ); // open "M1.txt" to read data
    readstrM1="";
    M1last=0;
    if (myFile) {      
      char characterM1;
      while ((characterM1 = myFile.read()) != -1) { // this while loop reads data stored in "M1.txt" and prints it to serial monitor
        
        readstrM1=readstrM1+characterM1;
      }
  
        }
     
        Serial.println ("M1 String from SD: " + readstrM1 );
        M1last = readstrM1.toInt();
        Serial.print("M1last: ");
        Serial.println(M1last);
        myFile.close();
        
        myFile = SD.open("M1.txt", FILE_WRITE);
        M1last = 3456789;
        myFile.println(M1last); // write number to file
       
        myFile.close();
  
  }
  

  SD.remove("M1.txt");
  
  myFile = SD.open("M1.txt", FILE_WRITE);
        M1last = 3456789;
        myFile.println(M1last); // write number to file
       
        myFile.close();      
        
      
     

  if (SD.exists("M2.txt")) {

    myFile = SD.open("M2.txt", FILE_READ); // open "M2.txt" to read data
    readstrM2="";
    M2last=0;
    if (myFile) {
      
      char characterM2;
      while ((characterM2 = myFile.read()) != -1) { // this while loop reads data stored in "M2.txt" and prints it to serial monitor
        
        readstrM2=readstrM2+characterM2;
      }
  
        
        //        Serial.write (readstrM2);
        Serial.println ("M2 String from SD: " + readstrM2 );
        M2last = readstrM2.toInt();
        Serial.print("M2last: ");
        Serial.println(M2last);
        myFile.close();
 
  }
        SD.remove("M2.txt");
        myFile = SD.open("M2.txt", FILE_WRITE);
        M2last = 9876543;
        myFile.println(M2last); // write number to file
       
        myFile.close();
  

  }

}

  void loop() {
    // nothing happens after setup finishes.
  }

Did you try running it twice? You open a file, read the contents and then print it out. AFTER you do that, you assign a new value and write it to the file. The serial monitor never shows you what is in the file after you have done that step. Also, if the file M1.txt or M2.txt doesn't exist on the SD card, nothing will happen because all your reading/writing is wrapped inside an if() statement.

blh64:
Did you try running it twice? You open a file, read the contents and then print it out. AFTER you do that, you assign a new value and write it to the file. The serial monitor never shows you what is in the file after you have done that step. Also, if the file M1.txt or M2.txt doesn't exist on the SD card, nothing will happen because all your reading/writing is wrapped inside an if() statement.

I pressed the reset button on the arduino several times and get the same results.
Also, there is another write script outside the IF loop for both M1 and M2 in case the files don't exist.

cosmicone:
I pressed the reset button on the arduino several times and get the same results.
Also, there is another write script outside the IF loop for both M1 and M2 in case the files don't exist.

The M2 write is inside the conditional, not the outside.