Put input from .txt file into an array

I'm trying to make a device to keep track of the amount of drinks me and my roommates take from the fridge. I got it working, but at the moment the names are defined in the code. However, I would prefer to read the names from a .txt file on a SD card.

The .txt file has the following format:

Freddy\n
Bambi\n
Gertje\n
Loekie\n
Mattie\n
Johnny\n
Floem\n
Elmo\n
Edt\n
Ronnie\n
Pieter\n
Vlaaij\n
Bram\n
Kruimel\n
Tommie\n
Lynn\n
Teddy

How i do this right now:

  • Make an array of size 25 (most likely there will be no more then 25 names to keep track of)
  • Fill this array with the names from the .txt file
  • Use the array in the rest of the code
//Outside the setup because I need these further down in the code
const int pinCS = 53;     //pin for SD card initialization
String huisnamen0[25]; //Array to store the names

void setup() {
  //Start the serial communication
  Serial.begin(9600);

  //Define pinCS output
  pinMode(pinCS, OUTPUT);

  //Initialise SD card
  SD.begin(pinCS);

  char inputCharString[20];   //To keep track of each read character
  char inputChar;                  //To store the last read character
  int stringIndex = 0;            //To keep track of the place in the inputCharString

  //Open the .txt file
  myFile = SD.open("HUISNA~1.TXT");
  //Make sure it has opened before running the following code
  if (myFile) {
    Serial.print("huisnamen.txt:");
    
    //Something left to read
    while (myFile.available()) {

      //Get the next character
      inputChar = myFile.read();
      
      //Check the type of the character
      if ((inputChar >= 'a' && inputChar <= 'z') || (inputChar >= 'A' && inputChar <= 'Z') || (inputChar >= '0' && inputChar <= '9')) {

        //Put the read character in the string on the correct place
        inputCharString[stringIndex] = inputChar;
        //Increase the index to put the next character in the next spot
        stringIndex++;

        //For debugging print some info to the serial monitor
        Serial.println("stringIndex: " + String(stringIndex)); 
        Serial.println("inputString: " + String(inputCharString));
      }
      
      //If an enter is encountered it means the end of the line and the name is reached
      else if (inputChar == '\n') {
        
        //For debugging print the name that is just read
        Serial.print("De naam is: ");
        Serial.println(inputCharString);
                
        //Put the read name into the array that stores the names
        huisnamen0[huisnamenIndex] = inputCharString;
        
        //Increase the index to put the next name in the next spot
        huisnamenIndex++;
        
        //Put the index for the inputCharString back to 0. This means the first character of the next name will again be put on the first spot of the inputCharString
        stringIndex = 0;
        //Reset the inputCharString array in case the next name is shorter than the last
        memset (inputCharString, 0, sizeof(inputCharString));
      }

    }
    //Exit the while loop because the .txt file is empty
    //Because the last name is not followed by an enter input the last name into the huisnamen0 array
    huisnamen0[huisnamenIndex] = inputCharString;

    //Done with the file so close it
    myFile.close();

    //For debugging print each value in the huisnamen0 array to check if it has read the names correctly 
    for(int i = 0; i < 24; i++) {
      Serial.println("huisnamenIndex: " + String(i));
      Serial.println("Huisnaam: " + String(huisnamen0[i]));
    }
  } 
  //If the file didn't open, print an error:
  else {
    Serial.println("error opening huisnamen.txt");
  }
}

But the problem with this is that if I put less than 25 names into the .txt file, for example 15, the last 10 entries of the huisnamen0 array will be empty and cause errors in the rest of my program.

So what I would like to do is:

  • Initialize an array with size 25

  • Fill it with the names from the .txt file

  • Resize the array to the correct size

  • Use the array in the rest of the code
    or

  • Initialize an array of undefined size

  • Fill it with the names from the .txt file

  • Use the array in the rest of the code
    or

  • First check how many names there are in the .txt file

  • Initialize an array with this size

  • Fill it with the names from the .txt file

  • Use the array in the rest of the code

Even after spending a few evenings doing research on how to tackle this I haven't gotten any closer to an answer. I've tried to be as clear as possible, so I hope anyone can help me!

Just keep the actual number of names you read from the file in a variable and use that instead of the array size. As long as you have less that 25 names, you should be ok.

If I do this I get the error that the array size isn't a constant integer.

No, the array is a constant size. How much you fill the array is in a variable. They are 2 different things.

For example, an array of size 25 could be filled by 20 names, in which case the variable contains 20 and all the loops use the variable as the sizing/loop limit parameter.

Ooh okay that makes sense. Thanks I will try this!