I think my program may be resetting, but not sure how/why?

Since a lot of print statements, seems to use up memory rapidly. I thought I might put my print statement text into a simple text file, and then read that text into a single character array (or string) during the running of the program. Each statements text is on a separate line, and I wanted to create a script to retrieve the text from the SD’card on line ###, and print it.
Print statements seem to be a key to debugging the programs, so looking for an alternate way to keep them in.

When I run the retrieval program, I only get 4 lines read and then it appears the program resets itself. Is that because of an error, use of the while(1); (which I thought should stop the program from running), or something else. Here is the simple code I am running, and the MONITOR results. The text file is attached.

Anyone of any ideas for this noob?
code:

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

const int chipSelect = 10;

void setup()
{
  int k;
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
    
   if (!SD.begin(10)) 
   {
     Serial.println(F("SD Card initialization failed!"));
    
   } else {
     Serial.println(F("SD Card initialization done."));
   }
   delay(100);


  k = PRINT(10);        //Search for contents of the 10line of the file
  Serial.print(F("Finished reading sd card'n"));
  while(1);
}

void loop() 
{
  //do nothing 
}


int PRINT(int txtPtr) {
  int LineCount=0;
  char ch;
  char TextString[30];
  int pointer=0;
  

   Serial.println(F("Attempt to open Strings.csv file.\n"));
   
     // open the file. note that only one file can be open at a time,
      // so you have to close this one before opening another.
   File dataFile = SD.open("Strings.csv",FILE_READ);
    
   // if the file is NOT available, stop program..
   if (! dataFile) {
     Serial.println(F("ERROR cant open Strings.csv file!"));
     while(1);
   }
    Serial.println(F("Reading from file"));
   
    while (dataFile.available()) 
    {
          ch = dataFile.read();      //read a character from the file
          
       if (ch != '\n')        //read characters into TextString[] array.
       {  
          TextString[pointer++] = ch;      //build up TextString with characters in the line.
          Serial.print(ch);
       }
       else
       {  
         Serial.println();      //Have now read in a full line.  
         
        //ch is  now \n
        if (LineCount == txtPtr) {    //if this the line we are searching for?
          
          for (int j=0; j <= (pointer-1); j++) {    //get rid of ###, at start of the line.
            TextString[j] = TextString[j+3];
          } 
          TextString[pointer]='\0';
          
          Serial.println(TextString);        //Again print out the full line 
          pointer=0;    
          memset(TextString,0,sizeof(TextString));      //erase the character array

          
          return(true);   //show search was successful.
        } //endi if Linecount
        
        //on to the next line
        LineCount++;

      }// endif ch != \n
    } // while dataFile is available
      
     //' dataFile.close();
      delay(5000);
      return(false);
}

Com Port View:

SD Card initialization done.
Attempt to open Strings.csv file.

Reading from file
001,"."

002,“SD_CSV_ReadWrite_attempt_v3\n\n”

003,“SETUP: Initializing SD card…”

004,"SETUP: Card iniSD Card initialization done.
Attempt to open Strings.csv file.

Reading from file
001,"."

002,“SD_CSV_ReadWrite_attempt_v3\n\n”

003,“SETUP: Initializing SD card…”

004,"SETUP: Card iniSD Card initialization done.
Attempt to open Strings.csv file.

Reading from file
001,"."

002,“SD_CSV_ReadWrite_attempt_v3\n\n”

003,“SETUP: Initializing SD card…”

004,"SETUP: Card iniSD Card initialization done.
Attempt to open Strings.csv file.

This continue to loop forever…

Strings.csv (1.09 KB)

I think this is your problem. You should reset the value of pointer back to zero if you haven't reached the correct line yet. So if LineCount is less than txtPtr you need to zero pointer. Otherwise you'll over-run the end of your string.

So your code should look more like:

       if (ch != '\n')        //read characters into TextString[] array.
       {  
         TextString[pointer++] = ch;      //build up TextString with characters in the line.
         [...]
       }
       else
       {  
         [...]
         
         //ch is  now \n
         if (LineCount == txtPtr) {    //if this the line we are searching for?
           [...]
          } 
         else {
           pointer = 0;
         }
        }

Otherwise after each line you keep adding data to the end of the array because pointer never gets reset. So it doesn't take much to overrun TextString's 30 bytes.

I'd also be more careful at adding characters to TextString blindly. As my pre-edited post stressed, it's possible to overrun your buffer if you don't keep track of how many characters you've received.

Regards,

Brad KF7FER