Populating an 2 dimensional array from SD Card

Hi everyone!

First time posting here, I have a good background of PHP behind me but since I’ve started Arduino thing aren’t easy as PHP…

Here is the situation:

  1. Open a text file from the SD card of the eth shield: CHECK

  2. Read and split the information of each row into two different variable: CHECK (from some code on this forum)

  3. Make an array out of the two diffrent variable, the first is the array index and the second data is the content of the same index: Unable

Here is my txt file:

1|White Rhum|
2|Dark Rhum|
3|Vodka|
4|Maraschino|
5|Tequila|
6|Southern Comfort|
7|Gin|
8|Cognac|
9|Malibu Rhum|
10|Triple Sec|
11|Amaretto|
12|Blue Curacao|

Here is the code:

  //Skipped the SD card declaration part

  char c, array[20];
  int counter, counter2;
  int limiteur;
  int id_will_be;
  int bottle_name[11][20];


  // 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("bottleL.txt");
  counter=0;
  counter2=0;
  limiteur = 0;
  id_will_be = 0;
  // if the file is available, write to it:
  if (dataFile) 
  {
    while (dataFile.available()) 
    {
      c = dataFile.read();
      if(c=='|')
      {
        if (counter >0)
        {
          limiteur++;
          counter2++;
          counter=0;
          if(limiteur <= 1)
          {
            //First value "id_will_be" => array inde
            id_will_be = atoi(array);
          }
          else if (limiteur == 2) // Second time a | si found
          {
            //Second value => Name of the bottle
            Serial.print(id_will_be); //Print the good index
            Serial.println(array); //Print the good name

            //Send each character as an int to the array with the previlousy found index
            bottle_name[id_will_be][20] = array[20];
            Serial.println(""); //to make some space
            limiteur = 0;      
          }
        }
      }        
      else
      {
        array[counter++] = c;
        array[counter] = 0;
      } 
    }

dataFile.close();
    
    Serial.println(""); //to make some space
    //Print the char from the int of each character of the selected index
    for(int j = 0; j <= 20; j++){
      Serial.print(char(bottle_name[2][j]));
    }
  //The actual ouput of this is:  "wrk Rhum  o  rt ô ?Vw"
  } 
  else 
  {
    Serial.println("Unable to open file");
  }

}
void loop()
{
}

A big thanks for your time!

L.

You can't assign like this:

            bottle_name[id_will_be][20] = array[20];

Look up strcpy.

Also wouldn't it be easier to read a line (eg. getline) rather than a character at a time?

Hi!

Thanks for the quick reply!

The name part is working with strcpy. I did see a lot of the function in my research, but i wasn't able get correctly my char variable at the time!

As for the getline it will be for tomorrow (1:03 AM here..), but rapidly I didn't find good reference or docs :/ The closer I got was the "Stream" class the Arduino have but doesn't seems to be implanted in the SD class.

L.

In the SDfat library there is, at least:

  const int maxLine = 80;
  char buffer[maxLine];
  ifstream sdin (fName);

...

  while (sdin.getline (buffer, maxLine))
    {
...

In the SD.h file that is part of the Arduino distribution:

class File : public Stream {