Reading an SD card, one line at a time.

Im certain this is in the reference section, and probably in the forum too, but for the life of me i cant find it! maybe my search box skills are not cut out for this forum. :blush:

I want to read an SD card text file one line at a time. I am using the arduino to process a Gcode file (saved as a .TXT rather than .NGC)

I think the best approach is to be buffer one line at a time, using /n. (using a char string perhaps?)
I can then discard the lines that dont start with G"ii" and make an action based on the ones that do.
I figure i would then go one line at a time in the loop, but would make sure it would progress to the next time each time the loop cycles, rather than repeating line one.....

would someone for who this is easy possibly outlay what i would need?

Read from SD card then buffer to a string??

So i got it to read the first line! but now it isnt cycling, it reads the first line then stops.
can anyone see why?
I was expecting this to serialprint individual lines (between Gs in this case) with a 1 second delay in between...

void loop() {

  
  inputChar = myFile.read(); // Gets one byte from serial buffer
  if (inputChar != 'G') // define breaking char here (\n isnt working for some reason, i will follow this up later)
    {
    inputString[stringIndex] = inputChar; // Store it
    stringIndex++; // Increment where to write next
  }
  else  
  {
    Serial.print("test: "); // shows that the program is cycling, for debugging only
    Serial.println(inputString);
    delay (1000);
    stringIndex = 0; // clear the value for the next cycle
    inputString [100]; // clear the value for the next cycle?
  }
}
    inputString [100]; // clear the value for the next cycle?

No, it doesn't.

inputString is not NULL terminated, so you should not pass it to a function that expects a string.

Testing that there is still data to read would be good.
Putting each { on a new line, and using Tools + Auto Format would be good.
Posting ALL of your code would be good.

Otherwise, we're just shooting in the dark trying to help you. You don't one someone to accidentally shoot you, do you?

I didn't include the rest of the code as there is a lot of excess guff to look through, which will be used later (the sketch is based on a preceding version)
I got a "no changes to make" response from autoformat and as far as i can see the curly braces are on their own lines apart from the ones at the "voids"
Thanks for looking.

//LIBRARIES
#include <SD.h>
#include <AFMotor.h>

//CONNECTIONS
File myFile; // instance of a file
const int chipSelect = 15; // adafruit SD breakout, wired 15 - 18. must use modified SD library to allow for reassignment of pins.
AF_Stepper motorL(200, 1);  // Left Motor, M1 & M2
AF_Stepper motorR(200, 2);  // Right Motor, M3 & M4 // Forward is Up on both motors.
const int button = 13; //button holds the sketch in setup, until pressed. This stops the motors from moving under USB power while uploading.
const int led = 14;
const int relay = 2;

// WORKING VALUES
char inputString [100];
char inputChar;
int stringIndex = 0; // String stringIndexing int;

void setup(){

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // setup
  pinMode (led, OUTPUT);
  pinMode (button, INPUT);
  pinMode (relay, OUTPUT); 
  motorL.setSpeed(20);  // 10 rpm   
  motorR.setSpeed(20);  // 10 rpm   

  Serial.print("Motors ready, Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(15,16,17,18)) 
{
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("card initialized.");

  // Open up the file we're going to log to!
  myFile = SD.open("HELLO.txt");
  if (! myFile)
 {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
  digitalWrite (led, HIGH);
  Serial.println("Waiting...");
  //hold
  while (digitalRead (button) == HIGH)
{
    // stops script. Its waiting for a button press (LOW on "button")
  }
  Serial.println("....Running");
}
void loop() {

  inputChar = myFile.read(); // Gets one byte from serial buffer
  if (inputChar != 'G') // define breaking char here (\n isnt working for some reason, i will follow this up later)
  {
    inputString[stringIndex] = inputChar; // Store it
    stringIndex++; // Increment where to write next
  }
  else  
  {
    Serial.print("test: "); // shows that the program is cycling, for debugging only
    Serial.println(inputString);
    delay (1000);
    stringIndex = 0; // clear the value for the next cycle
  }
}

It seems to be because the String wasnt long enough to accept the next line. Sorted it. Onto the next issue!