How do i read the last data from the SD card file.

Hi.
I want to know how to read the last data from the file in the SD card.
So that i can use that value to run a stepper motor.
the positions of the stepper is stored by push buttons
Like a memory for the stepper.

here is the code.
this is what i got so far i am unable to proceed.

Im still a noob.
Help is greatly appreciated

// defines pins numbers
const int stepPin = 5;//clk++
const int dirPin  = 2;//cw++
const int enPin  = 8;//en+
// Button
const int b1 = A0;
const int b2 = A1;
const int b3 = A2;
const int b4 = A3;
const int chipSelect = 4;
int currentAngle = 0;
int angle = 0;
float stepPerAngle = 1.8; // full step = 1.8
int   numstep;
#include <SPI.h>
#include <SD.h>
void setup() {
 Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
 }


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

 // see if the card is present and can be initialized:
 if (!SD.begin(chipSelect)) {
   Serial.println("Card failed, or not present");
   // don't do anything more:
   while (1);
 
 Serial.println("card initialized.");
 File dataFile = SD.open("datalog.txt");

 // if the file is available, write to it:
 if (dataFile) {
   while (dataFile.available()) {
     Serial.write(dataFile.read());
   }
   dataFile.close();
 }
 // if the file isn't open, pop up an error:
 else {
   Serial.println("error opening datalog.txt");
 }
}

 }
{
 // Sets the two pins as Outputs
 pinMode(stepPin,OUTPUT);
 pinMode(dirPin,OUTPUT);

 pinMode(enPin,OUTPUT);
 digitalWrite(enPin,LOW);//on
 digitalWrite(dirPin,HIGH);

 pinMode(b1, INPUT);
 pinMode(b2, INPUT);
 pinMode(b3, INPUT);
 pinMode(b4, INPUT);

}
}
void loop() {

   int n;
   if( digitalRead(b1) == HIGH){
       angle = 0;
      
   }
   else if( digitalRead(b2) == HIGH){
       angle = 90;
      
   }
   else if( digitalRead(b3) == HIGH){
       angle = 180;
      
   }
   else if( digitalRead(b4) == HIGH){
       angle = 270;
      
   }

   if( currentAngle != angle ){
       if( currentAngle < angle){
           digitalWrite(dirPin,HIGH);//clockwise
            n = angle - currentAngle;
           numstep = n / stepPerAngle;
       }
       else if( currentAngle > angle){
           digitalWrite(dirPin,LOW);//anticlockwise
           n = currentAngle - angle;
           if( angle == 0){
              n =currentAngle;
           }
           numstep = n / stepPerAngle;
       }

       for(int x = 0; x < numstep; x++) {
          digitalWrite(stepPin,HIGH);
          delayMicroseconds(1000);
          digitalWrite(stepPin,LOW);
          delayMicroseconds(1000);
       }

       currentAngle = angle;
   }

  delay(500);

File dataFile = SD.open("datalog.txt", FILE_WRITE);

 // if the file is available, write to it:
 if (dataFile) {
   dataFile.println(angle);
   dataFile.close();
   // print to the serial port too:
   Serial.println(angle);
 }
 // if the file isn't open, pop up an error:
 else {
   Serial.println("error opening datalog.txt");
 }
}

Have not read your code but to read the last entry, You use seek() to set the position in the file at the right location, which will be the file size minus the size of your data and then you read the last record (First byte is position 0)

Say your file is 100 bytes long (pos 0 to 99), and your data 4 bytes long, You position the head to 100-4=96 and you read 4 bytes (at positions 96,97,98,99)

can you please give me an example code. thank you

I'll correct what you try --> write some code:

here are the functions to use: use open() to open a file in read mode use size() to get the size of the file use sizeof() to calculate the size of the data you want to read use seek() to position yourself at the end minus the length of the data size you want to read use the file.read(buf, len) version of read() to read the right number of bytes into a buffer. use close() to close the file properly.