1

Hello have an array of strings that loads strings from a txt file on an sd card. The txt file on the SD card can change from time to time(not while the Arduino is running).I want to be able to get the amount of elements in the array after the txt file has been loaded into the array. The txt file has lines of txt in it. Each line in the file is put into a string in the array. I want to be able to get the amount of elements in the array how can I do this?

6

You can't. That concept does not exist in C.

An array has a fixed size - as such you know how many elements are in it, since that is the number of elements you created it with.

String myStrings[4];

That is an array of 4 String objects. It has 4 elements. It will always have 4 elements.

  • 1
    so could I set the array size to say 400 and only load 100 lines of text? – user2279603 Feb 17 '16 at 11:39
  • 2
    If you have the memory to store that kind of thing. – Majenko Feb 17 '16 at 11:39
5

numElements = (sizeof(array) / sizeof(array[0]));

2

@user2279603

This is your third post on basically the same subject. If I attempt to "read-between-the-lines" you are trying to implement a simple shell.

You need to rethink how command lines are entered. It is not a very good idea to read the whole file into a vector of strings. Even worse is using the String class.

Cheers!

protected by VE7JRO Oct 26 at 1:47

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.