read TXT File from SD Card into array

i need to read values from sd card, the txt value separated by comma e.g 01,02,03 . how to create array with separator comma . I am very new to programming and think the values would either be array of string or array of char. Please help! Thanks

Hi

Loading string values into an array might be problematic if you cannot pre-define the size of the arrays and the length of each string field. Using Arduino Strings or char arrays to process the load would likely both be problematic.

If the data to read is, say just one or two lines and maybe six or ten fields (variables), it may not be too much of a problem - but might lead to some free heap fragmentation if you do not predefine the length of the array variables.

This untested program may give you some ideas:

//Program to read a csv file 
//Parsing comma separated fields 
//We assume all the fields on each line are strings
//This implementation only expects two string variabes on each line
//and ten rows (lines) of inout data.
#include <SPI.h>
#include <SD.h>

const int C_RowCount = 10;
const int C_ColCount = 2;
String G_2D_Strings[C_RowCount][C_ColCount];

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

  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // re-open the file for reading:
  File myFile = SD.open("TEST.CSV");
  if (!myFile) {
    Serial.println("error opening test.txt");
    return;
  }

  //Initialise the 2D string array - just in case
  for (int l_row = 0; l_row < C_RowCount; l_row++) {
    for (int l_col = 0; l_row < C_ColCount; l_row++) {
      G_2D_Strings[l_row][l_col] = "";
    }
  }

  Serial.println("test.txt file opened");

  // read from the file until there's nothing else in it:
  String l_line;
  l_line.reserve(128); //Avoids heap memory fragmentation
                       //Reserve space for your longest expected data line
  int l_row = 0;
  while (myFile.available()) {
    l_line = myFile.readStringUntil('\n');
    l_line.trim();
    if (l_line != "") {
      int l_start_posn = 0;
      int l_col = 0;
      while (l_start_posn != -1) {
        G_2D_Strings[l_row][l_col] = ENDF2(l_line,l_start_posn,',');
        l_col++;
        //Abandon the rest of the line after we have taken two column values for our array
        if (l_col == C_ColCount)
          break;
        //
      }
      l_row++;
      //Exit when we have filled the array (ten rows max)
      if (l_row == C_RowCount)
        break;
      //
    } //skip blank (NULL) lines
  }//Read the file line by line
  myFile.close();
  Serial.println("test.txt file loaded and closed");
    
} //setup

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

String ENDF2(String &p_line, int &p_start, char p_delimiter) {
//EXTRACT NEXT DELIMITED FIELD VERSION 2
//Extract fields from a line one at a time based on a delimiter.
//Because the line remains intact we dont fragment heap memory
//p_start would normally start as 0
//p_start increments as we move along the line
//We return p_start = -1 with the last field

  //If we have already parsed the whole line then return null
  if (p_start == -1) {
    return "";
  }

  int l_start = p_start;
  int l_index = p_line.indexOf(p_delimiter,l_start);
  if (l_index == -1) { //last field of the data line
    p_start = l_index;
    return p_line.substring(l_start);
  }
  else { //take the next field off the data line
    p_start = l_index + 1;
    return p_line.substring(l_start,l_index); //Include, Exclude
  }
}

The G_2D_Strings array assumes ten lines of comma separated data and only two string values on each field.

You may have to do more work on the ENDF2 procedure if your string fields are quoted to permit embedded comma string characters (not comma delimiters).

As indicated above this implementation may cause a chunk of free heap memory that will never release (but can be used by other procedures).

If you are actually loading integer arrays using the String.toint() function with a predefined int array size then you should not suffer any ongoing free heap memory fragmentation during the load process.

Cheers

Catweazle NZ

Here is a way to read CSV files without using String.